1. Project Clover database Fri Apr 26 2024 18:06:03 CDT
  2. Package org.apache.commons.lang3

File ArrayUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

1,264
1,963
320
1
8,674
3,257
1,049
0.53
6.13
320
3.28

Classes

Class Line # Actions
ArrayUtils 46 1,963 0% 1,049 83
0.9765999397.7%
 

Contributing tests

This file is covered by 669 tests. .

Source view

1    /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10    *
11    * Unless required by applicable law or agreed to in writing, software
12    * distributed under the License is distributed on an "AS IS" BASIS,
13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    * See the License for the specific language governing permissions and
15    * limitations under the License.
16    */
17    package org.apache.commons.lang3;
18   
19    import java.lang.reflect.Array;
20    import java.util.Arrays;
21    import java.util.BitSet;
22    import java.util.Comparator;
23    import java.util.HashMap;
24    import java.util.Map;
25    import java.util.Random;
26   
27    import org.apache.commons.lang3.builder.EqualsBuilder;
28    import org.apache.commons.lang3.builder.HashCodeBuilder;
29    import org.apache.commons.lang3.builder.ToStringBuilder;
30    import org.apache.commons.lang3.builder.ToStringStyle;
31    import org.apache.commons.lang3.math.NumberUtils;
32    import org.apache.commons.lang3.mutable.MutableInt;
33   
34    /**
35    * <p>Operations on arrays, primitive arrays (like {@code int[]}) and
36    * primitive wrapper arrays (like {@code Integer[]}).
37    *
38    * <p>This class tries to handle {@code null} input gracefully.
39    * An exception will not be thrown for a {@code null}
40    * array input. However, an Object array that contains a {@code null}
41    * element may throw an exception. Each method documents its behaviour.
42    *
43    * <p>#ThreadSafe#
44    * @since 2.0
45    */
 
46    public class ArrayUtils {
47   
48    /**
49    * An empty immutable {@code Object} array.
50    */
51    public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
52    /**
53    * An empty immutable {@code Class} array.
54    */
55    public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
56    /**
57    * An empty immutable {@code String} array.
58    */
59    public static final String[] EMPTY_STRING_ARRAY = new String[0];
60    /**
61    * An empty immutable {@code long} array.
62    */
63    public static final long[] EMPTY_LONG_ARRAY = new long[0];
64    /**
65    * An empty immutable {@code Long} array.
66    */
67    public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
68    /**
69    * An empty immutable {@code int} array.
70    */
71    public static final int[] EMPTY_INT_ARRAY = new int[0];
72    /**
73    * An empty immutable {@code Integer} array.
74    */
75    public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
76    /**
77    * An empty immutable {@code short} array.
78    */
79    public static final short[] EMPTY_SHORT_ARRAY = new short[0];
80    /**
81    * An empty immutable {@code Short} array.
82    */
83    public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
84    /**
85    * An empty immutable {@code byte} array.
86    */
87    public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
88    /**
89    * An empty immutable {@code Byte} array.
90    */
91    public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
92    /**
93    * An empty immutable {@code double} array.
94    */
95    public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
96    /**
97    * An empty immutable {@code Double} array.
98    */
99    public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
100    /**
101    * An empty immutable {@code float} array.
102    */
103    public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
104    /**
105    * An empty immutable {@code Float} array.
106    */
107    public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
108    /**
109    * An empty immutable {@code boolean} array.
110    */
111    public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
112    /**
113    * An empty immutable {@code Boolean} array.
114    */
115    public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
116    /**
117    * An empty immutable {@code char} array.
118    */
119    public static final char[] EMPTY_CHAR_ARRAY = new char[0];
120    /**
121    * An empty immutable {@code Character} array.
122    */
123    public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
124   
125    /**
126    * The index value when an element is not found in a list or array: {@code -1}.
127    * This value is returned by methods in this class and can also be used in comparisons with values returned by
128    * various method from {@link java.util.List}.
129    */
130    public static final int INDEX_NOT_FOUND = -1;
131   
132    /**
133    * <p>ArrayUtils instances should NOT be constructed in standard programming.
134    * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>.
135    *
136    * <p>This constructor is public to permit tools that require a JavaBean instance
137    * to operate.
138    */
 
139  1 toggle public ArrayUtils() {
140  1 super();
141    }
142   
143   
144    // NOTE: Cannot use {@code} to enclose text which includes {}, but <code></code> is OK
145   
146   
147    // Basic methods handling multi-dimensional arrays
148    //-----------------------------------------------------------------------
149    /**
150    * <p>Outputs an array as a String, treating {@code null} as an empty array.
151    *
152    * <p>Multi-dimensional arrays are handled correctly, including
153    * multi-dimensional primitive arrays.
154    *
155    * <p>The format is that of Java source code, for example <code>{a,b}</code>.
156    *
157    * @param array the array to get a toString for, may be {@code null}
158    * @return a String representation of the array, '{}' if null array input
159    */
 
160  13 toggle public static String toString(final Object array) {
161  13 return toString(array, "{}");
162    }
163   
164    /**
165    * <p>Outputs an array as a String handling {@code null}s.
166    *
167    * <p>Multi-dimensional arrays are handled correctly, including
168    * multi-dimensional primitive arrays.
169    *
170    * <p>The format is that of Java source code, for example <code>{a,b}</code>.
171    *
172    * @param array the array to get a toString for, may be {@code null}
173    * @param stringIfNull the String to return if the array is {@code null}
174    * @return a String representation of the array
175    */
 
176  18 toggle public static String toString(final Object array, final String stringIfNull) {
177  18 if (array == null) {
178  2 return stringIfNull;
179    }
180  16 return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
181    }
182   
183    /**
184    * <p>Get a hash code for an array handling multi-dimensional arrays correctly.
185    *
186    * <p>Multi-dimensional primitive arrays are also handled correctly by this method.
187    *
188    * @param array the array to get a hash code for, {@code null} returns zero
189    * @return a hash code for the array
190    */
 
191  10 toggle public static int hashCode(final Object array) {
192  10 return new HashCodeBuilder().append(array).toHashCode();
193    }
194   
195    /**
196    * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
197    * correctly.
198    *
199    * <p>Multi-dimensional primitive arrays are also handled correctly by this method.
200    *
201    * @param array1 the left hand array to compare, may be {@code null}
202    * @param array2 the right hand array to compare, may be {@code null}
203    * @return {@code true} if the arrays are equal
204    * @deprecated this method has been replaced by {@code java.util.Objects.deepEquals(Object, Object)} and will be
205    * removed from future releases.
206    */
 
207  132 toggle @Deprecated
208    public static boolean isEquals(final Object array1, final Object array2) {
209  132 return new EqualsBuilder().append(array1, array2).isEquals();
210    }
211   
212    // To map
213    //-----------------------------------------------------------------------
214    /**
215    * <p>Converts the given array into a {@link java.util.Map}. Each element of the array
216    * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
217    * elements, where the first element is used as key and the second as
218    * value.
219    *
220    * <p>This method can be used to initialize:
221    * <pre>
222    * // Create a Map mapping colors.
223    * Map colorMap = ArrayUtils.toMap(new String[][] {
224    * {"RED", "#FF0000"},
225    * {"GREEN", "#00FF00"},
226    * {"BLUE", "#0000FF"}});
227    * </pre>
228    *
229    * <p>This method returns {@code null} for a {@code null} input array.
230    *
231    * @param array an array whose elements are either a {@link java.util.Map.Entry} or
232    * an Array containing at least two elements, may be {@code null}
233    * @return a {@code Map} that was created from the array
234    * @throws IllegalArgumentException if one element of this Array is
235    * itself an Array containing less then two elements
236    * @throws IllegalArgumentException if the array contains elements other
237    * than {@link java.util.Map.Entry} and an Array
238    */
 
239  6 toggle public static Map<Object, Object> toMap(final Object[] array) {
240  6 if (array == null) {
241  1 return null;
242    }
243  5 final Map<Object, Object> map = new HashMap<>((int) (array.length * 1.5));
244  11 for (int i = 0; i < array.length; i++) {
245  9 final Object object = array[i];
246  9 if (object instanceof Map.Entry<?, ?>) {
247  1 final Map.Entry<?,?> entry = (Map.Entry<?,?>) object;
248  1 map.put(entry.getKey(), entry.getValue());
249  8 } else if (object instanceof Object[]) {
250  6 final Object[] entry = (Object[]) object;
251  6 if (entry.length < 2) {
252  1 throw new IllegalArgumentException("Array element " + i + ", '"
253    + object
254    + "', has a length less than 2");
255    }
256  5 map.put(entry[0], entry[1]);
257    } else {
258  2 throw new IllegalArgumentException("Array element " + i + ", '"
259    + object
260    + "', is neither of type Map.Entry nor an Array");
261    }
262    }
263  2 return map;
264    }
265   
266    // Generic array
267    //-----------------------------------------------------------------------
268    /**
269    * <p>Create a type-safe generic array.
270    *
271    * <p>The Java language does not allow an array to be created from a generic type:
272    *
273    * <pre>
274    public static &lt;T&gt; T[] createAnArray(int size) {
275    return new T[size]; // compiler error here
276    }
277    public static &lt;T&gt; T[] createAnArray(int size) {
278    return (T[])new Object[size]; // ClassCastException at runtime
279    }
280    * </pre>
281    *
282    * <p>Therefore new arrays of generic types can be created with this method.
283    * For example, an array of Strings can be created:
284    *
285    * <pre>
286    String[] array = ArrayUtils.toArray("1", "2");
287    String[] emptyArray = ArrayUtils.&lt;String&gt;toArray();
288    * </pre>
289    *
290    * <p>The method is typically used in scenarios, where the caller itself uses generic types
291    * that have to be combined into an array.
292    *
293    * <p>Note, this method makes only sense to provide arguments of the same type so that the
294    * compiler can deduce the type of the array itself. While it is possible to select the
295    * type explicitly like in
296    * <code>Number[] array = ArrayUtils.&lt;Number&gt;toArray(Integer.valueOf(42), Double.valueOf(Math.PI))</code>,
297    * there is no real advantage when compared to
298    * <code>new Number[] {Integer.valueOf(42), Double.valueOf(Math.PI)}</code>.
299    *
300    * @param <T> the array's element type
301    * @param items the varargs array items, null allowed
302    * @return the array, not null unless a null array is passed in
303    * @since 3.0
304    */
 
305  6 toggle public static <T> T[] toArray(final T... items) {
306  6 return items;
307    }
308   
309    // Clone
310    //-----------------------------------------------------------------------
311    /**
312    * <p>Shallow clones an array returning a typecast result and handling
313    * {@code null}.
314    *
315    * <p>The objects in the array are not cloned, thus there is no special
316    * handling for multi-dimensional arrays.
317    *
318    * <p>This method returns {@code null} for a {@code null} input array.
319    *
320    * @param <T> the component type of the array
321    * @param array the array to shallow clone, may be {@code null}
322    * @return the cloned array, {@code null} if {@code null} input
323    */
 
324  35 toggle public static <T> T[] clone(final T[] array) {
325  35 if (array == null) {
326  6 return null;
327    }
328  29 return array.clone();
329    }
330   
331    /**
332    * <p>Clones an array returning a typecast result and handling
333    * {@code null}.
334    *
335    * <p>This method returns {@code null} for a {@code null} input array.
336    *
337    * @param array the array to clone, may be {@code null}
338    * @return the cloned array, {@code null} if {@code null} input
339    */
 
340  56 toggle public static long[] clone(final long[] array) {
341  56 if (array == null) {
342  5 return null;
343    }
344  51 return array.clone();
345    }
346   
347    /**
348    * <p>Clones an array returning a typecast result and handling
349    * {@code null}.
350    *
351    * <p>This method returns {@code null} for a {@code null} input array.
352    *
353    * @param array the array to clone, may be {@code null}
354    * @return the cloned array, {@code null} if {@code null} input
355    */
 
356  90197 toggle public static int[] clone(final int[] array) {
357  90197 if (array == null) {
358  5 return null;
359    }
360  90192 return array.clone();
361    }
362   
363    /**
364    * <p>Clones an array returning a typecast result and handling
365    * {@code null}.
366    *
367    * <p>This method returns {@code null} for a {@code null} input array.
368    *
369    * @param array the array to clone, may be {@code null}
370    * @return the cloned array, {@code null} if {@code null} input
371    */
 
372  16 toggle public static short[] clone(final short[] array) {
373  16 if (array == null) {
374  5 return null;
375    }
376  11 return array.clone();
377    }
378   
379    /**
380    * <p>Clones an array returning a typecast result and handling
381    * {@code null}.
382    *
383    * <p>This method returns {@code null} for a {@code null} input array.
384    *
385    * @param array the array to clone, may be {@code null}
386    * @return the cloned array, {@code null} if {@code null} input
387    */
 
388  38 toggle public static char[] clone(final char[] array) {
389  38 if (array == null) {
390  10 return null;
391    }
392  28 return array.clone();
393    }
394   
395    /**
396    * <p>Clones an array returning a typecast result and handling
397    * {@code null}.
398    *
399    * <p>This method returns {@code null} for a {@code null} input array.
400    *
401    * @param array the array to clone, may be {@code null}
402    * @return the cloned array, {@code null} if {@code null} input
403    */
 
404  16 toggle public static byte[] clone(final byte[] array) {
405  16 if (array == null) {
406  5 return null;
407    }
408  11 return array.clone();
409    }
410   
411    /**
412    * <p>Clones an array returning a typecast result and handling
413    * {@code null}.
414    *
415    * <p>This method returns {@code null} for a {@code null} input array.
416    *
417    * @param array the array to clone, may be {@code null}
418    * @return the cloned array, {@code null} if {@code null} input
419    */
 
420  16 toggle public static double[] clone(final double[] array) {
421  16 if (array == null) {
422  5 return null;
423    }
424  11 return array.clone();
425    }
426   
427    /**
428    * <p>Clones an array returning a typecast result and handling
429    * {@code null}.
430    *
431    * <p>This method returns {@code null} for a {@code null} input array.
432    *
433    * @param array the array to clone, may be {@code null}
434    * @return the cloned array, {@code null} if {@code null} input
435    */
 
436  16 toggle public static float[] clone(final float[] array) {
437  16 if (array == null) {
438  5 return null;
439    }
440  11 return array.clone();
441    }
442   
443    /**
444    * <p>Clones an array returning a typecast result and handling
445    * {@code null}.
446    *
447    * <p>This method returns {@code null} for a {@code null} input array.
448    *
449    * @param array the array to clone, may be {@code null}
450    * @return the cloned array, {@code null} if {@code null} input
451    */
 
452  15 toggle public static boolean[] clone(final boolean[] array) {
453  15 if (array == null) {
454  5 return null;
455    }
456  10 return array.clone();
457    }
458   
459    // nullToEmpty
460    //-----------------------------------------------------------------------
461    /**
462    * <p>Defensive programming technique to change a {@code null}
463    * reference to an empty one.
464    *
465    * <p>This method returns an empty array for a {@code null} input array.
466    *
467    * @param array the array to check for {@code null} or empty
468    * @param type the class representation of the desired array
469    * @param <T> the class type
470    * @return the same array, {@code public static} empty array if {@code null}
471    * @throws IllegalArgumentException if the type argument is null
472    * @since 3.5
473    */
 
474  4 toggle public static <T> T[] nullToEmpty(final T[] array, final Class<T[]> type) {
475  4 if (type == null) {
476  1 throw new IllegalArgumentException("The type must not be null");
477    }
478   
479  3 if (array == null) {
480  1 return type.cast(Array.newInstance(type.getComponentType(), 0));
481    }
482  2 return array;
483    }
484   
485   
486    /**
487    * <p>Defensive programming technique to change a {@code null}
488    * reference to an empty one.
489    *
490    * <p>This method returns an empty array for a {@code null} input array.
491    *
492    * <p>As a memory optimizing technique an empty array passed in will be overridden with
493    * the empty {@code public static} references in this class.
494    *
495    * @param array the array to check for {@code null} or empty
496    * @return the same array, {@code public static} empty array if {@code null} or empty input
497    * @since 2.5
498    */
 
499  332 toggle public static Object[] nullToEmpty(final Object[] array) {
500  332 if (isEmpty(array)) {
501  129 return EMPTY_OBJECT_ARRAY;
502    }
503  203 return array;
504    }
505   
506    /**
507    * <p>Defensive programming technique to change a {@code null}
508    * reference to an empty one.
509    *
510    * <p>This method returns an empty array for a {@code null} input array.
511    *
512    * <p>As a memory optimizing technique an empty array passed in will be overridden with
513    * the empty {@code public static} references in this class.
514    *
515    * @param array the array to check for {@code null} or empty
516    * @return the same array, {@code public static} empty array if {@code null} or empty input
517    * @since 3.2
518    */
 
519  214 toggle public static Class<?>[] nullToEmpty(final Class<?>[] array) {
520  214 if (isEmpty(array)) {
521  112 return EMPTY_CLASS_ARRAY;
522    }
523  102 return array;
524    }
525   
526    /**
527    * <p>Defensive programming technique to change a {@code null}
528    * reference to an empty one.
529    *
530    * <p>This method returns an empty array for a {@code null} input array.
531    *
532    * <p>As a memory optimizing technique an empty array passed in will be overridden with
533    * the empty {@code public static} references in this class.
534    *
535    * @param array the array to check for {@code null} or empty
536    * @return the same array, {@code public static} empty array if {@code null} or empty input
537    * @since 2.5
538    */
 
539  3 toggle public static String[] nullToEmpty(final String[] array) {
540  3 if (isEmpty(array)) {
541  2 return EMPTY_STRING_ARRAY;
542    }
543  1 return array;
544    }
545   
546    /**
547    * <p>Defensive programming technique to change a {@code null}
548    * reference to an empty one.
549    *
550    * <p>This method returns an empty array for a {@code null} input array.
551    *
552    * <p>As a memory optimizing technique an empty array passed in will be overridden with
553    * the empty {@code public static} references in this class.
554    *
555    * @param array the array to check for {@code null} or empty
556    * @return the same array, {@code public static} empty array if {@code null} or empty input
557    * @since 2.5
558    */
 
559  3 toggle public static long[] nullToEmpty(final long[] array) {
560  3 if (isEmpty(array)) {
561  2 return EMPTY_LONG_ARRAY;
562    }
563  1 return array;
564    }
565   
566    /**
567    * <p>Defensive programming technique to change a {@code null}
568    * reference to an empty one.
569    *
570    * <p>This method returns an empty array for a {@code null} input array.
571    *
572    * <p>As a memory optimizing technique an empty array passed in will be overridden with
573    * the empty {@code public static} references in this class.
574    *
575    * @param array the array to check for {@code null} or empty
576    * @return the same array, {@code public static} empty array if {@code null} or empty input
577    * @since 2.5
578    */
 
579  3 toggle public static int[] nullToEmpty(final int[] array) {
580  3 if (isEmpty(array)) {
581  2 return EMPTY_INT_ARRAY;
582    }
583  1 return array;
584    }
585   
586    /**
587    * <p>Defensive programming technique to change a {@code null}
588    * reference to an empty one.
589    *
590    * <p>This method returns an empty array for a {@code null} input array.
591    *
592    * <p>As a memory optimizing technique an empty array passed in will be overridden with
593    * the empty {@code public static} references in this class.
594    *
595    * @param array the array to check for {@code null} or empty
596    * @return the same array, {@code public static} empty array if {@code null} or empty input
597    * @since 2.5
598    */
 
599  3 toggle public static short[] nullToEmpty(final short[] array) {
600  3 if (isEmpty(array)) {
601  2 return EMPTY_SHORT_ARRAY;
602    }
603  1 return array;
604    }
605   
606    /**
607    * <p>Defensive programming technique to change a {@code null}
608    * reference to an empty one.
609    *
610    * <p>This method returns an empty array for a {@code null} input array.
611    *
612    * <p>As a memory optimizing technique an empty array passed in will be overridden with
613    * the empty {@code public static} references in this class.
614    *
615    * @param array the array to check for {@code null} or empty
616    * @return the same array, {@code public static} empty array if {@code null} or empty input
617    * @since 2.5
618    */
 
619  3 toggle public static char[] nullToEmpty(final char[] array) {
620  3 if (isEmpty(array)) {
621  2 return EMPTY_CHAR_ARRAY;
622    }
623  1 return array;
624    }
625   
626    /**
627    * <p>Defensive programming technique to change a {@code null}
628    * reference to an empty one.
629    *
630    * <p>This method returns an empty array for a {@code null} input array.
631    *
632    * <p>As a memory optimizing technique an empty array passed in will be overridden with
633    * the empty {@code public static} references in this class.
634    *
635    * @param array the array to check for {@code null} or empty
636    * @return the same array, {@code public static} empty array if {@code null} or empty input
637    * @since 2.5
638    */
 
639  3 toggle public static byte[] nullToEmpty(final byte[] array) {
640  3 if (isEmpty(array)) {
641  2 return EMPTY_BYTE_ARRAY;
642    }
643  1 return array;
644    }
645   
646    /**
647    * <p>Defensive programming technique to change a {@code null}
648    * reference to an empty one.
649    *
650    * <p>This method returns an empty array for a {@code null} input array.
651    *
652    * <p>As a memory optimizing technique an empty array passed in will be overridden with
653    * the empty {@code public static} references in this class.
654    *
655    * @param array the array to check for {@code null} or empty
656    * @return the same array, {@code public static} empty array if {@code null} or empty input
657    * @since 2.5
658    */
 
659  3 toggle public static double[] nullToEmpty(final double[] array) {
660  3 if (isEmpty(array)) {
661  2 return EMPTY_DOUBLE_ARRAY;
662    }
663  1 return array;
664    }
665   
666    /**
667    * <p>Defensive programming technique to change a {@code null}
668    * reference to an empty one.
669    *
670    * <p>This method returns an empty array for a {@code null} input array.
671    *
672    * <p>As a memory optimizing technique an empty array passed in will be overridden with
673    * the empty {@code public static} references in this class.
674    *
675    * @param array the array to check for {@code null} or empty
676    * @return the same array, {@code public static} empty array if {@code null} or empty input
677    * @since 2.5
678    */
 
679  3 toggle public static float[] nullToEmpty(final float[] array) {
680  3 if (isEmpty(array)) {
681  2 return EMPTY_FLOAT_ARRAY;
682    }
683  1 return array;
684    }
685   
686    /**
687    * <p>Defensive programming technique to change a {@code null}
688    * reference to an empty one.
689    *
690    * <p>This method returns an empty array for a {@code null} input array.
691    *
692    * <p>As a memory optimizing technique an empty array passed in will be overridden with
693    * the empty {@code public static} references in this class.
694    *
695    * @param array the array to check for {@code null} or empty
696    * @return the same array, {@code public static} empty array if {@code null} or empty input
697    * @since 2.5
698    */
 
699  3 toggle public static boolean[] nullToEmpty(final boolean[] array) {
700  3 if (isEmpty(array)) {
701  2 return EMPTY_BOOLEAN_ARRAY;
702    }
703  1 return array;
704    }
705   
706    /**
707    * <p>Defensive programming technique to change a {@code null}
708    * reference to an empty one.
709    *
710    * <p>This method returns an empty array for a {@code null} input array.
711    *
712    * <p>As a memory optimizing technique an empty array passed in will be overridden with
713    * the empty {@code public static} references in this class.
714    *
715    * @param array the array to check for {@code null} or empty
716    * @return the same array, {@code public static} empty array if {@code null} or empty input
717    * @since 2.5
718    */
 
719  3 toggle public static Long[] nullToEmpty(final Long[] array) {
720  3 if (isEmpty(array)) {
721  2 return EMPTY_LONG_OBJECT_ARRAY;
722    }
723  1 return array;
724    }
725   
726    /**
727    * <p>Defensive programming technique to change a {@code null}
728    * reference to an empty one.
729    *
730    * <p>This method returns an empty array for a {@code null} input array.
731    *
732    * <p>As a memory optimizing technique an empty array passed in will be overridden with
733    * the empty {@code public static} references in this class.
734    *
735    * @param array the array to check for {@code null} or empty
736    * @return the same array, {@code public static} empty array if {@code null} or empty input
737    * @since 2.5
738    */
 
739  3 toggle public static Integer[] nullToEmpty(final Integer[] array) {
740  3 if (isEmpty(array)) {
741  2 return EMPTY_INTEGER_OBJECT_ARRAY;
742    }
743  1 return array;
744    }
745   
746    /**
747    * <p>Defensive programming technique to change a {@code null}
748    * reference to an empty one.
749    *
750    * <p>This method returns an empty array for a {@code null} input array.
751    *
752    * <p>As a memory optimizing technique an empty array passed in will be overridden with
753    * the empty {@code public static} references in this class.
754    *
755    * @param array the array to check for {@code null} or empty
756    * @return the same array, {@code public static} empty array if {@code null} or empty input
757    * @since 2.5
758    */
 
759  3 toggle public static Short[] nullToEmpty(final Short[] array) {
760  3 if (isEmpty(array)) {
761  2 return EMPTY_SHORT_OBJECT_ARRAY;
762    }
763  1 return array;
764    }
765   
766    /**
767    * <p>Defensive programming technique to change a {@code null}
768    * reference to an empty one.
769    *
770    * <p>This method returns an empty array for a {@code null} input array.
771    *
772    * <p>As a memory optimizing technique an empty array passed in will be overridden with
773    * the empty {@code public static} references in this class.
774    *
775    * @param array the array to check for {@code null} or empty
776    * @return the same array, {@code public static} empty array if {@code null} or empty input
777    * @since 2.5
778    */
 
779  3 toggle public static Character[] nullToEmpty(final Character[] array) {
780  3 if (isEmpty(array)) {
781  2 return EMPTY_CHARACTER_OBJECT_ARRAY;
782    }
783  1 return array;
784    }
785   
786    /**
787    * <p>Defensive programming technique to change a {@code null}
788    * reference to an empty one.
789    *
790    * <p>This method returns an empty array for a {@code null} input array.
791    *
792    * <p>As a memory optimizing technique an empty array passed in will be overridden with
793    * the empty {@code public static} references in this class.
794    *
795    * @param array the array to check for {@code null} or empty
796    * @return the same array, {@code public static} empty array if {@code null} or empty input
797    * @since 2.5
798    */
 
799  3 toggle public static Byte[] nullToEmpty(final Byte[] array) {
800  3 if (isEmpty(array)) {
801  2 return EMPTY_BYTE_OBJECT_ARRAY;
802    }
803  1 return array;
804    }
805   
806    /**
807    * <p>Defensive programming technique to change a {@code null}
808    * reference to an empty one.
809    *
810    * <p>This method returns an empty array for a {@code null} input array.
811    *
812    * <p>As a memory optimizing technique an empty array passed in will be overridden with
813    * the empty {@code public static} references in this class.
814    *
815    * @param array the array to check for {@code null} or empty
816    * @return the same array, {@code public static} empty array if {@code null} or empty input
817    * @since 2.5
818    */
 
819  3 toggle public static Double[] nullToEmpty(final Double[] array) {
820  3 if (isEmpty(array)) {
821  2 return EMPTY_DOUBLE_OBJECT_ARRAY;
822    }
823  1 return array;
824    }
825   
826    /**
827    * <p>Defensive programming technique to change a {@code null}
828    * reference to an empty one.
829    *
830    * <p>This method returns an empty array for a {@code null} input array.
831    *
832    * <p>As a memory optimizing technique an empty array passed in will be overridden with
833    * the empty {@code public static} references in this class.
834    *
835    * @param array the array to check for {@code null} or empty
836    * @return the same array, {@code public static} empty array if {@code null} or empty input
837    * @since 2.5
838    */
 
839  3 toggle public static Float[] nullToEmpty(final Float[] array) {
840  3 if (isEmpty(array)) {
841  2 return EMPTY_FLOAT_OBJECT_ARRAY;
842    }
843  1 return array;
844    }
845   
846    /**
847    * <p>Defensive programming technique to change a {@code null}
848    * reference to an empty one.
849    *
850    * <p>This method returns an empty array for a {@code null} input array.
851    *
852    * <p>As a memory optimizing technique an empty array passed in will be overridden with
853    * the empty {@code public static} references in this class.
854    *
855    * @param array the array to check for {@code null} or empty
856    * @return the same array, {@code public static} empty array if {@code null} or empty input
857    * @since 2.5
858    */
 
859  3 toggle public static Boolean[] nullToEmpty(final Boolean[] array) {
860  3 if (isEmpty(array)) {
861  2 return EMPTY_BOOLEAN_OBJECT_ARRAY;
862    }
863  1 return array;
864    }
865   
866    // Subarrays
867    //-----------------------------------------------------------------------
868    /**
869    * <p>Produces a new array containing the elements between
870    * the start and end indices.
871    *
872    * <p>The start index is inclusive, the end index exclusive.
873    * Null array input produces null output.
874    *
875    * <p>The component type of the subarray is always the same as
876    * that of the input array. Thus, if the input is an array of type
877    * {@code Date}, the following usage is envisaged:
878    *
879    * <pre>
880    * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
881    * </pre>
882    *
883    * @param <T> the component type of the array
884    * @param array the array
885    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
886    * is promoted to 0, overvalue (&gt;array.length) results
887    * in an empty array.
888    * @param endIndexExclusive elements up to endIndex-1 are present in the
889    * returned subarray. Undervalue (&lt; startIndex) produces
890    * empty array, overvalue (&gt;array.length) is demoted to
891    * array length.
892    * @return a new array containing the elements between
893    * the start and end indices.
894    * @since 2.1
895    * @see Arrays#copyOfRange(Object[], int, int)
896    */
 
897  16 toggle public static <T> T[] subarray(final T[] array, int startIndexInclusive, int endIndexExclusive) {
898  16 if (array == null) {
899  1 return null;
900    }
901  15 if (startIndexInclusive < 0) {
902  2 startIndexInclusive = 0;
903    }
904  15 if (endIndexExclusive > array.length) {
905  3 endIndexExclusive = array.length;
906    }
907  15 final int newSize = endIndexExclusive - startIndexInclusive;
908  15 final Class<?> type = array.getClass().getComponentType();
909  15 if (newSize <= 0) {
910  4 @SuppressWarnings("unchecked") // OK, because array is of type T
911    final T[] emptyArray = (T[]) Array.newInstance(type, 0);
912  4 return emptyArray;
913    }
914  11 @SuppressWarnings("unchecked") // OK, because array is of type T
915    final
916    T[] subarray = (T[]) Array.newInstance(type, newSize);
917  11 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
918  11 return subarray;
919    }
920   
921    /**
922    * <p>Produces a new {@code long} array containing the elements
923    * between the start and end indices.
924    *
925    * <p>The start index is inclusive, the end index exclusive.
926    * Null array input produces null output.
927    *
928    * @param array the array
929    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
930    * is promoted to 0, overvalue (&gt;array.length) results
931    * in an empty array.
932    * @param endIndexExclusive elements up to endIndex-1 are present in the
933    * returned subarray. Undervalue (&lt; startIndex) produces
934    * empty array, overvalue (&gt;array.length) is demoted to
935    * array length.
936    * @return a new array containing the elements between
937    * the start and end indices.
938    * @since 2.1
939    * @see Arrays#copyOfRange(long[], int, int)
940    */
 
941  17 toggle public static long[] subarray(final long[] array, int startIndexInclusive, int endIndexExclusive) {
942  17 if (array == null) {
943  1 return null;
944    }
945  16 if (startIndexInclusive < 0) {
946  2 startIndexInclusive = 0;
947    }
948  16 if (endIndexExclusive > array.length) {
949  4 endIndexExclusive = array.length;
950    }
951  16 final int newSize = endIndexExclusive - startIndexInclusive;
952  16 if (newSize <= 0) {
953  8 return EMPTY_LONG_ARRAY;
954    }
955   
956  8 final long[] subarray = new long[newSize];
957  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
958  8 return subarray;
959    }
960   
961    /**
962    * <p>Produces a new {@code int} array containing the elements
963    * between the start and end indices.
964    *
965    * <p>The start index is inclusive, the end index exclusive.
966    * Null array input produces null output.
967    *
968    * @param array the array
969    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
970    * is promoted to 0, overvalue (&gt;array.length) results
971    * in an empty array.
972    * @param endIndexExclusive elements up to endIndex-1 are present in the
973    * returned subarray. Undervalue (&lt; startIndex) produces
974    * empty array, overvalue (&gt;array.length) is demoted to
975    * array length.
976    * @return a new array containing the elements between
977    * the start and end indices.
978    * @since 2.1
979    * @see Arrays#copyOfRange(int[], int, int)
980    */
 
981  17 toggle public static int[] subarray(final int[] array, int startIndexInclusive, int endIndexExclusive) {
982  17 if (array == null) {
983  1 return null;
984    }
985  16 if (startIndexInclusive < 0) {
986  2 startIndexInclusive = 0;
987    }
988  16 if (endIndexExclusive > array.length) {
989  4 endIndexExclusive = array.length;
990    }
991  16 final int newSize = endIndexExclusive - startIndexInclusive;
992  16 if (newSize <= 0) {
993  8 return EMPTY_INT_ARRAY;
994    }
995   
996  8 final int[] subarray = new int[newSize];
997  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
998  8 return subarray;
999    }
1000   
1001    /**
1002    * <p>Produces a new {@code short} array containing the elements
1003    * between the start and end indices.
1004    *
1005    * <p>The start index is inclusive, the end index exclusive.
1006    * Null array input produces null output.
1007    *
1008    * @param array the array
1009    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
1010    * is promoted to 0, overvalue (&gt;array.length) results
1011    * in an empty array.
1012    * @param endIndexExclusive elements up to endIndex-1 are present in the
1013    * returned subarray. Undervalue (&lt; startIndex) produces
1014    * empty array, overvalue (&gt;array.length) is demoted to
1015    * array length.
1016    * @return a new array containing the elements between
1017    * the start and end indices.
1018    * @since 2.1
1019    * @see Arrays#copyOfRange(short[], int, int)
1020    */
 
1021  17 toggle public static short[] subarray(final short[] array, int startIndexInclusive, int endIndexExclusive) {
1022  17 if (array == null) {
1023  1 return null;
1024    }
1025  16 if (startIndexInclusive < 0) {
1026  2 startIndexInclusive = 0;
1027    }
1028  16 if (endIndexExclusive > array.length) {
1029  4 endIndexExclusive = array.length;
1030    }
1031  16 final int newSize = endIndexExclusive - startIndexInclusive;
1032  16 if (newSize <= 0) {
1033  8 return EMPTY_SHORT_ARRAY;
1034    }
1035   
1036  8 final short[] subarray = new short[newSize];
1037  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
1038  8 return subarray;
1039    }
1040   
1041    /**
1042    * <p>Produces a new {@code char} array containing the elements
1043    * between the start and end indices.
1044    *
1045    * <p>The start index is inclusive, the end index exclusive.
1046    * Null array input produces null output.
1047    *
1048    * @param array the array
1049    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
1050    * is promoted to 0, overvalue (&gt;array.length) results
1051    * in an empty array.
1052    * @param endIndexExclusive elements up to endIndex-1 are present in the
1053    * returned subarray. Undervalue (&lt; startIndex) produces
1054    * empty array, overvalue (&gt;array.length) is demoted to
1055    * array length.
1056    * @return a new array containing the elements between
1057    * the start and end indices.
1058    * @since 2.1
1059    * @see Arrays#copyOfRange(char[], int, int)
1060    */
 
1061  17 toggle public static char[] subarray(final char[] array, int startIndexInclusive, int endIndexExclusive) {
1062  17 if (array == null) {
1063  1 return null;
1064    }
1065  16 if (startIndexInclusive < 0) {
1066  2 startIndexInclusive = 0;
1067    }
1068  16 if (endIndexExclusive > array.length) {
1069  4 endIndexExclusive = array.length;
1070    }
1071  16 final int newSize = endIndexExclusive - startIndexInclusive;
1072  16 if (newSize <= 0) {
1073  8 return EMPTY_CHAR_ARRAY;
1074    }
1075   
1076  8 final char[] subarray = new char[newSize];
1077  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
1078  8 return subarray;
1079    }
1080   
1081    /**
1082    * <p>Produces a new {@code byte} array containing the elements
1083    * between the start and end indices.
1084    *
1085    * <p>The start index is inclusive, the end index exclusive.
1086    * Null array input produces null output.
1087    *
1088    * @param array the array
1089    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
1090    * is promoted to 0, overvalue (&gt;array.length) results
1091    * in an empty array.
1092    * @param endIndexExclusive elements up to endIndex-1 are present in the
1093    * returned subarray. Undervalue (&lt; startIndex) produces
1094    * empty array, overvalue (&gt;array.length) is demoted to
1095    * array length.
1096    * @return a new array containing the elements between
1097    * the start and end indices.
1098    * @since 2.1
1099    * @see Arrays#copyOfRange(byte[], int, int)
1100    */
 
1101  17 toggle public static byte[] subarray(final byte[] array, int startIndexInclusive, int endIndexExclusive) {
1102  17 if (array == null) {
1103  1 return null;
1104    }
1105  16 if (startIndexInclusive < 0) {
1106  2 startIndexInclusive = 0;
1107    }
1108  16 if (endIndexExclusive > array.length) {
1109  4 endIndexExclusive = array.length;
1110    }
1111  16 final int newSize = endIndexExclusive - startIndexInclusive;
1112  16 if (newSize <= 0) {
1113  8 return EMPTY_BYTE_ARRAY;
1114    }
1115   
1116  8 final byte[] subarray = new byte[newSize];
1117  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
1118  8 return subarray;
1119    }
1120   
1121    /**
1122    * <p>Produces a new {@code double} array containing the elements
1123    * between the start and end indices.
1124    *
1125    * <p>The start index is inclusive, the end index exclusive.
1126    * Null array input produces null output.
1127    *
1128    * @param array the array
1129    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
1130    * is promoted to 0, overvalue (&gt;array.length) results
1131    * in an empty array.
1132    * @param endIndexExclusive elements up to endIndex-1 are present in the
1133    * returned subarray. Undervalue (&lt; startIndex) produces
1134    * empty array, overvalue (&gt;array.length) is demoted to
1135    * array length.
1136    * @return a new array containing the elements between
1137    * the start and end indices.
1138    * @since 2.1
1139    * @see Arrays#copyOfRange(double[], int, int)
1140    */
 
1141  17 toggle public static double[] subarray(final double[] array, int startIndexInclusive, int endIndexExclusive) {
1142  17 if (array == null) {
1143  1 return null;
1144    }
1145  16 if (startIndexInclusive < 0) {
1146  2 startIndexInclusive = 0;
1147    }
1148  16 if (endIndexExclusive > array.length) {
1149  4 endIndexExclusive = array.length;
1150    }
1151  16 final int newSize = endIndexExclusive - startIndexInclusive;
1152  16 if (newSize <= 0) {
1153  8 return EMPTY_DOUBLE_ARRAY;
1154    }
1155   
1156  8 final double[] subarray = new double[newSize];
1157  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
1158  8 return subarray;
1159    }
1160   
1161    /**
1162    * <p>Produces a new {@code float} array containing the elements
1163    * between the start and end indices.
1164    *
1165    * <p>The start index is inclusive, the end index exclusive.
1166    * Null array input produces null output.
1167    *
1168    * @param array the array
1169    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
1170    * is promoted to 0, overvalue (&gt;array.length) results
1171    * in an empty array.
1172    * @param endIndexExclusive elements up to endIndex-1 are present in the
1173    * returned subarray. Undervalue (&lt; startIndex) produces
1174    * empty array, overvalue (&gt;array.length) is demoted to
1175    * array length.
1176    * @return a new array containing the elements between
1177    * the start and end indices.
1178    * @since 2.1
1179    * @see Arrays#copyOfRange(float[], int, int)
1180    */
 
1181  17 toggle public static float[] subarray(final float[] array, int startIndexInclusive, int endIndexExclusive) {
1182  17 if (array == null) {
1183  1 return null;
1184    }
1185  16 if (startIndexInclusive < 0) {
1186  2 startIndexInclusive = 0;
1187    }
1188  16 if (endIndexExclusive > array.length) {
1189  4 endIndexExclusive = array.length;
1190    }
1191  16 final int newSize = endIndexExclusive - startIndexInclusive;
1192  16 if (newSize <= 0) {
1193  8 return EMPTY_FLOAT_ARRAY;
1194    }
1195   
1196  8 final float[] subarray = new float[newSize];
1197  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
1198  8 return subarray;
1199    }
1200   
1201    /**
1202    * <p>Produces a new {@code boolean} array containing the elements
1203    * between the start and end indices.
1204    *
1205    * <p>The start index is inclusive, the end index exclusive.
1206    * Null array input produces null output.
1207    *
1208    * @param array the array
1209    * @param startIndexInclusive the starting index. Undervalue (&lt;0)
1210    * is promoted to 0, overvalue (&gt;array.length) results
1211    * in an empty array.
1212    * @param endIndexExclusive elements up to endIndex-1 are present in the
1213    * returned subarray. Undervalue (&lt; startIndex) produces
1214    * empty array, overvalue (&gt;array.length) is demoted to
1215    * array length.
1216    * @return a new array containing the elements between
1217    * the start and end indices.
1218    * @since 2.1
1219    * @see Arrays#copyOfRange(boolean[], int, int)
1220    */
 
1221  17 toggle public static boolean[] subarray(final boolean[] array, int startIndexInclusive, int endIndexExclusive) {
1222  17 if (array == null) {
1223  1 return null;
1224    }
1225  16 if (startIndexInclusive < 0) {
1226  2 startIndexInclusive = 0;
1227    }
1228  16 if (endIndexExclusive > array.length) {
1229  4 endIndexExclusive = array.length;
1230    }
1231  16 final int newSize = endIndexExclusive - startIndexInclusive;
1232  16 if (newSize <= 0) {
1233  8 return EMPTY_BOOLEAN_ARRAY;
1234    }
1235   
1236  8 final boolean[] subarray = new boolean[newSize];
1237  8 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
1238  8 return subarray;
1239    }
1240   
1241    // Is same length
1242    //-----------------------------------------------------------------------
1243    /**
1244    * <p>Checks whether two arrays are the same length, treating
1245    * {@code null} arrays as length {@code 0}.
1246    *
1247    * <p>Any multi-dimensional aspects of the arrays are ignored.
1248    *
1249    * @param array1 the first array, may be {@code null}
1250    * @param array2 the second array, may be {@code null}
1251    * @return {@code true} if length of arrays matches, treating
1252    * {@code null} as an empty array
1253    */
 
1254  409 toggle public static boolean isSameLength(final Object[] array1, final Object[] array2) {
1255  409 return getLength(array1) == getLength(array2);
1256    }
1257   
1258    /**
1259    * <p>Checks whether two arrays are the same length, treating
1260    * {@code null} arrays as length {@code 0}.
1261    *
1262    * @param array1 the first array, may be {@code null}
1263    * @param array2 the second array, may be {@code null}
1264    * @return {@code true} if length of arrays matches, treating
1265    * {@code null} as an empty array
1266    */
 
1267  16 toggle public static boolean isSameLength(final long[] array1, final long[] array2) {
1268  16 return getLength(array1) == getLength(array2);
1269    }
1270   
1271    /**
1272    * <p>Checks whether two arrays are the same length, treating
1273    * {@code null} arrays as length {@code 0}.
1274    *
1275    * @param array1 the first array, may be {@code null}
1276    * @param array2 the second array, may be {@code null}
1277    * @return {@code true} if length of arrays matches, treating
1278    * {@code null} as an empty array
1279    */
 
1280  16 toggle public static boolean isSameLength(final int[] array1, final int[] array2) {
1281  16 return getLength(array1) == getLength(array2);
1282    }
1283   
1284    /**
1285    * <p>Checks whether two arrays are the same length, treating
1286    * {@code null} arrays as length {@code 0}.
1287    *
1288    * @param array1 the first array, may be {@code null}
1289    * @param array2 the second array, may be {@code null}
1290    * @return {@code true} if length of arrays matches, treating
1291    * {@code null} as an empty array
1292    */
 
1293  16 toggle public static boolean isSameLength(final short[] array1, final short[] array2) {
1294  16 return getLength(array1) == getLength(array2);
1295    }
1296   
1297    /**
1298    * <p>Checks whether two arrays are the same length, treating
1299    * {@code null} arrays as length {@code 0}.
1300    *
1301    * @param array1 the first array, may be {@code null}
1302    * @param array2 the second array, may be {@code null}
1303    * @return {@code true} if length of arrays matches, treating
1304    * {@code null} as an empty array
1305    */
 
1306  16 toggle public static boolean isSameLength(final char[] array1, final char[] array2) {
1307  16 return getLength(array1) == getLength(array2);
1308    }
1309   
1310    /**
1311    * <p>Checks whether two arrays are the same length, treating
1312    * {@code null} arrays as length {@code 0}.
1313    *
1314    * @param array1 the first array, may be {@code null}
1315    * @param array2 the second array, may be {@code null}
1316    * @return {@code true} if length of arrays matches, treating
1317    * {@code null} as an empty array
1318    */
 
1319  16 toggle public static boolean isSameLength(final byte[] array1, final byte[] array2) {
1320  16 return getLength(array1) == getLength(array2);
1321    }
1322   
1323    /**
1324    * <p>Checks whether two arrays are the same length, treating
1325    * {@code null} arrays as length {@code 0}.
1326    *
1327    * @param array1 the first array, may be {@code null}
1328    * @param array2 the second array, may be {@code null}
1329    * @return {@code true} if length of arrays matches, treating
1330    * {@code null} as an empty array
1331    */
 
1332  16 toggle public static boolean isSameLength(final double[] array1, final double[] array2) {
1333  16 return getLength(array1) == getLength(array2);
1334    }
1335   
1336    /**
1337    * <p>Checks whether two arrays are the same length, treating
1338    * {@code null} arrays as length {@code 0}.
1339    *
1340    * @param array1 the first array, may be {@code null}
1341    * @param array2 the second array, may be {@code null}
1342    * @return {@code true} if length of arrays matches, treating
1343    * {@code null} as an empty array
1344    */
 
1345  16 toggle public static boolean isSameLength(final float[] array1, final float[] array2) {
1346  16 return getLength(array1) == getLength(array2);
1347    }
1348   
1349    /**
1350    * <p>Checks whether two arrays are the same length, treating
1351    * {@code null} arrays as length {@code 0}.
1352    *
1353    * @param array1 the first array, may be {@code null}
1354    * @param array2 the second array, may be {@code null}
1355    * @return {@code true} if length of arrays matches, treating
1356    * {@code null} as an empty array
1357    */
 
1358  16 toggle public static boolean isSameLength(final boolean[] array1, final boolean[] array2) {
1359  16 return getLength(array1) == getLength(array2);
1360    }
1361   
1362    //-----------------------------------------------------------------------
1363    /**
1364    * <p>Returns the length of the specified array.
1365    * This method can deal with {@code Object} arrays and with primitive arrays.
1366    *
1367    * <p>If the input array is {@code null}, {@code 0} is returned.
1368    *
1369    * <pre>
1370    * ArrayUtils.getLength(null) = 0
1371    * ArrayUtils.getLength([]) = 0
1372    * ArrayUtils.getLength([null]) = 1
1373    * ArrayUtils.getLength([true, false]) = 2
1374    * ArrayUtils.getLength([1, 2, 3]) = 3
1375    * ArrayUtils.getLength(["a", "b", "c"]) = 3
1376    * </pre>
1377    *
1378    * @param array the array to retrieve the length from, may be null
1379    * @return The length of the array, or {@code 0} if the array is {@code null}
1380    * @throws IllegalArgumentException if the object argument is not an array.
1381    * @since 2.1
1382    */
 
1383  272939 toggle public static int getLength(final Object array) {
1384  272939 if (array == null) {
1385  306 return 0;
1386    }
1387  272633 return Array.getLength(array);
1388    }
1389   
1390    /**
1391    * <p>Checks whether two arrays are the same type taking into account
1392    * multi-dimensional arrays.
1393    *
1394    * @param array1 the first array, must not be {@code null}
1395    * @param array2 the second array, must not be {@code null}
1396    * @return {@code true} if type of arrays matches
1397    * @throws IllegalArgumentException if either array is {@code null}
1398    */
 
1399  8 toggle public static boolean isSameType(final Object array1, final Object array2) {
1400  8 if (array1 == null || array2 == null) {
1401  3 throw new IllegalArgumentException("The Array must not be null");
1402    }
1403  5 return array1.getClass().getName().equals(array2.getClass().getName());
1404    }
1405   
1406    // Reverse
1407    //-----------------------------------------------------------------------
1408    /**
1409    * <p>Reverses the order of the given array.
1410    *
1411    * <p>There is no special handling for multi-dimensional arrays.
1412    *
1413    * <p>This method does nothing for a {@code null} input array.
1414    *
1415    * @param array the array to reverse, may be {@code null}
1416    */
 
1417  7 toggle public static void reverse(final Object[] array) {
1418  7 if (array == null) {
1419  1 return;
1420    }
1421  6 reverse(array, 0, array.length);
1422    }
1423   
1424    /**
1425    * <p>Reverses the order of the given array.
1426    *
1427    * <p>This method does nothing for a {@code null} input array.
1428    *
1429    * @param array the array to reverse, may be {@code null}
1430    */
 
1431  71 toggle public static void reverse(final long[] array) {
1432  71 if (array == null) {
1433  1 return;
1434    }
1435  70 reverse(array, 0, array.length);
1436    }
1437   
1438    /**
1439    * <p>Reverses the order of the given array.
1440    *
1441    * <p>This method does nothing for a {@code null} input array.
1442    *
1443    * @param array the array to reverse, may be {@code null}
1444    */
 
1445  2 toggle public static void reverse(final int[] array) {
1446  2 if (array == null) {
1447  1 return;
1448    }
1449  1 reverse(array, 0, array.length);
1450    }
1451   
1452    /**
1453    * <p>Reverses the order of the given array.
1454    *
1455    * <p>This method does nothing for a {@code null} input array.
1456    *
1457    * @param array the array to reverse, may be {@code null}
1458    */
 
1459  2 toggle public static void reverse(final short[] array) {
1460  2 if (array == null) {
1461  1 return;
1462    }
1463  1 reverse(array, 0, array.length);
1464    }
1465   
1466    /**
1467    * <p>Reverses the order of the given array.
1468    *
1469    * <p>This method does nothing for a {@code null} input array.
1470    *
1471    * @param array the array to reverse, may be {@code null}
1472    */
 
1473  2 toggle public static void reverse(final char[] array) {
1474  2 if (array == null) {
1475  1 return;
1476    }
1477  1 reverse(array, 0, array.length);
1478    }
1479   
1480    /**
1481    * <p>Reverses the order of the given array.
1482    *
1483    * <p>This method does nothing for a {@code null} input array.
1484    *
1485    * @param array the array to reverse, may be {@code null}
1486    */
 
1487  2 toggle public static void reverse(final byte[] array) {
1488  2 if (array == null) {
1489  1 return;
1490    }
1491  1 reverse(array, 0, array.length);
1492    }
1493   
1494    /**
1495    * <p>Reverses the order of the given array.
1496    *
1497    * <p>This method does nothing for a {@code null} input array.
1498    *
1499    * @param array the array to reverse, may be {@code null}
1500    */
 
1501  2 toggle public static void reverse(final double[] array) {
1502  2 if (array == null) {
1503  1 return;
1504    }
1505  1 reverse(array, 0, array.length);
1506    }
1507   
1508    /**
1509    * <p>Reverses the order of the given array.
1510    *
1511    * <p>This method does nothing for a {@code null} input array.
1512    *
1513    * @param array the array to reverse, may be {@code null}
1514    */
 
1515  2 toggle public static void reverse(final float[] array) {
1516  2 if (array == null) {
1517  1 return;
1518    }
1519  1 reverse(array, 0, array.length);
1520    }
1521   
1522    /**
1523    * <p>Reverses the order of the given array.
1524    *
1525    * <p>This method does nothing for a {@code null} input array.
1526    *
1527    * @param array the array to reverse, may be {@code null}
1528    */
 
1529  2 toggle public static void reverse(final boolean[] array) {
1530  2 if (array == null) {
1531  1 return;
1532    }
1533  1 reverse(array, 0, array.length);
1534    }
1535   
1536    /**
1537    * <p>
1538    * Reverses the order of the given array in the given range.
1539    *
1540    * <p>
1541    * This method does nothing for a {@code null} input array.
1542    *
1543    * @param array
1544    * the array to reverse, may be {@code null}
1545    * @param startIndexInclusive
1546    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1547    * change.
1548    * @param endIndexExclusive
1549    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1550    * change. Overvalue (&gt;array.length) is demoted to array length.
1551    * @since 3.2
1552    */
 
1553  6 toggle public static void reverse(final boolean[] array, final int startIndexInclusive, final int endIndexExclusive) {
1554  6 if (array == null) {
1555  1 return;
1556    }
1557  5 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1558  5 int j = Math.min(array.length, endIndexExclusive) - 1;
1559  5 boolean tmp;
1560  10 while (j > i) {
1561  5 tmp = array[j];
1562  5 array[j] = array[i];
1563  5 array[i] = tmp;
1564  5 j--;
1565  5 i++;
1566    }
1567    }
1568   
1569    /**
1570    * <p>
1571    * Reverses the order of the given array in the given range.
1572    *
1573    * <p>
1574    * This method does nothing for a {@code null} input array.
1575    *
1576    * @param array
1577    * the array to reverse, may be {@code null}
1578    * @param startIndexInclusive
1579    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1580    * change.
1581    * @param endIndexExclusive
1582    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1583    * change. Overvalue (&gt;array.length) is demoted to array length.
1584    * @since 3.2
1585    */
 
1586  6 toggle public static void reverse(final byte[] array, final int startIndexInclusive, final int endIndexExclusive) {
1587  6 if (array == null) {
1588  1 return;
1589    }
1590  5 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1591  5 int j = Math.min(array.length, endIndexExclusive) - 1;
1592  5 byte tmp;
1593  10 while (j > i) {
1594  5 tmp = array[j];
1595  5 array[j] = array[i];
1596  5 array[i] = tmp;
1597  5 j--;
1598  5 i++;
1599    }
1600    }
1601   
1602    /**
1603    * <p>
1604    * Reverses the order of the given array in the given range.
1605    *
1606    * <p>
1607    * This method does nothing for a {@code null} input array.
1608    *
1609    * @param array
1610    * the array to reverse, may be {@code null}
1611    * @param startIndexInclusive
1612    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1613    * change.
1614    * @param endIndexExclusive
1615    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1616    * change. Overvalue (&gt;array.length) is demoted to array length.
1617    * @since 3.2
1618    */
 
1619  6 toggle public static void reverse(final char[] array, final int startIndexInclusive, final int endIndexExclusive) {
1620  6 if (array == null) {
1621  1 return;
1622    }
1623  5 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1624  5 int j = Math.min(array.length, endIndexExclusive) - 1;
1625  5 char tmp;
1626  10 while (j > i) {
1627  5 tmp = array[j];
1628  5 array[j] = array[i];
1629  5 array[i] = tmp;
1630  5 j--;
1631  5 i++;
1632    }
1633    }
1634   
1635    /**
1636    * <p>
1637    * Reverses the order of the given array in the given range.
1638    *
1639    * <p>
1640    * This method does nothing for a {@code null} input array.
1641    *
1642    * @param array
1643    * the array to reverse, may be {@code null}
1644    * @param startIndexInclusive
1645    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1646    * change.
1647    * @param endIndexExclusive
1648    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1649    * change. Overvalue (&gt;array.length) is demoted to array length.
1650    * @since 3.2
1651    */
 
1652  6 toggle public static void reverse(final double[] array, final int startIndexInclusive, final int endIndexExclusive) {
1653  6 if (array == null) {
1654  1 return;
1655    }
1656  5 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1657  5 int j = Math.min(array.length, endIndexExclusive) - 1;
1658  5 double tmp;
1659  10 while (j > i) {
1660  5 tmp = array[j];
1661  5 array[j] = array[i];
1662  5 array[i] = tmp;
1663  5 j--;
1664  5 i++;
1665    }
1666    }
1667   
1668    /**
1669    * <p>
1670    * Reverses the order of the given array in the given range.
1671    *
1672    * <p>
1673    * This method does nothing for a {@code null} input array.
1674    *
1675    * @param array
1676    * the array to reverse, may be {@code null}
1677    * @param startIndexInclusive
1678    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1679    * change.
1680    * @param endIndexExclusive
1681    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1682    * change. Overvalue (&gt;array.length) is demoted to array length.
1683    * @since 3.2
1684    */
 
1685  6 toggle public static void reverse(final float[] array, final int startIndexInclusive, final int endIndexExclusive) {
1686  6 if (array == null) {
1687  1 return;
1688    }
1689  5 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1690  5 int j = Math.min(array.length, endIndexExclusive) - 1;
1691  5 float tmp;
1692  10 while (j > i) {
1693  5 tmp = array[j];
1694  5 array[j] = array[i];
1695  5 array[i] = tmp;
1696  5 j--;
1697  5 i++;
1698    }
1699    }
1700   
1701    /**
1702    * <p>
1703    * Reverses the order of the given array in the given range.
1704    *
1705    * <p>
1706    * This method does nothing for a {@code null} input array.
1707    *
1708    * @param array
1709    * the array to reverse, may be {@code null}
1710    * @param startIndexInclusive
1711    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1712    * change.
1713    * @param endIndexExclusive
1714    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1715    * change. Overvalue (&gt;array.length) is demoted to array length.
1716    * @since 3.2
1717    */
 
1718  6 toggle public static void reverse(final int[] array, final int startIndexInclusive, final int endIndexExclusive) {
1719  6 if (array == null) {
1720  1 return;
1721    }
1722  5 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1723  5 int j = Math.min(array.length, endIndexExclusive) - 1;
1724  5 int tmp;
1725  10 while (j > i) {
1726  5 tmp = array[j];
1727  5 array[j] = array[i];
1728  5 array[i] = tmp;
1729  5 j--;
1730  5 i++;
1731    }
1732    }
1733   
1734    /**
1735    * <p>
1736    * Reverses the order of the given array in the given range.
1737    *
1738    * <p>
1739    * This method does nothing for a {@code null} input array.
1740    *
1741    * @param array
1742    * the array to reverse, may be {@code null}
1743    * @param startIndexInclusive
1744    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1745    * change.
1746    * @param endIndexExclusive
1747    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1748    * change. Overvalue (&gt;array.length) is demoted to array length.
1749    * @since 3.2
1750    */
 
1751  75 toggle public static void reverse(final long[] array, final int startIndexInclusive, final int endIndexExclusive) {
1752  75 if (array == null) {
1753  1 return;
1754    }
1755  74 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1756  74 int j = Math.min(array.length, endIndexExclusive) - 1;
1757  74 long tmp;
1758  99 while (j > i) {
1759  25 tmp = array[j];
1760  25 array[j] = array[i];
1761  25 array[i] = tmp;
1762  25 j--;
1763  25 i++;
1764    }
1765    }
1766   
1767    /**
1768    * <p>
1769    * Reverses the order of the given array in the given range.
1770    *
1771    * <p>
1772    * This method does nothing for a {@code null} input array.
1773    *
1774    * @param array
1775    * the array to reverse, may be {@code null}
1776    * @param startIndexInclusive
1777    * the starting index. Under value (&lt;0) is promoted to 0, over value (&gt;array.length) results in no
1778    * change.
1779    * @param endIndexExclusive
1780    * elements up to endIndex-1 are reversed in the array. Under value (&lt; start index) results in no
1781    * change. Over value (&gt;array.length) is demoted to array length.
1782    * @since 3.2
1783    */
 
1784  11 toggle public static void reverse(final Object[] array, final int startIndexInclusive, final int endIndexExclusive) {
1785  11 if (array == null) {
1786  1 return;
1787    }
1788  10 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1789  10 int j = Math.min(array.length, endIndexExclusive) - 1;
1790  10 Object tmp;
1791  18 while (j > i) {
1792  8 tmp = array[j];
1793  8 array[j] = array[i];
1794  8 array[i] = tmp;
1795  8 j--;
1796  8 i++;
1797    }
1798    }
1799   
1800    /**
1801    * <p>
1802    * Reverses the order of the given array in the given range.
1803    *
1804    * <p>
1805    * This method does nothing for a {@code null} input array.
1806    *
1807    * @param array
1808    * the array to reverse, may be {@code null}
1809    * @param startIndexInclusive
1810    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
1811    * change.
1812    * @param endIndexExclusive
1813    * elements up to endIndex-1 are reversed in the array. Undervalue (&lt; start index) results in no
1814    * change. Overvalue (&gt;array.length) is demoted to array length.
1815    * @since 3.2
1816    */
 
1817  6 toggle public static void reverse(final short[] array, final int startIndexInclusive, final int endIndexExclusive) {
1818  6 if (array == null) {
1819  1 return;
1820    }
1821  5 int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
1822  5 int j = Math.min(array.length, endIndexExclusive) - 1;
1823  5 short tmp;
1824  10 while (j > i) {
1825  5 tmp = array[j];
1826  5 array[j] = array[i];
1827  5 array[i] = tmp;
1828  5 j--;
1829  5 i++;
1830    }
1831    }
1832   
1833    // Swap
1834    //-----------------------------------------------------------------------
1835    /**
1836    * Swaps two elements in the given array.
1837    *
1838    * <p>There is no special handling for multi-dimensional arrays. This method
1839    * does nothing for a {@code null} or empty input array or for overflow indices.
1840    * Negative indices are promoted to 0(zero).</p>
1841    *
1842    * Examples:
1843    * <ul>
1844    * <li>ArrayUtils.swap(["1", "2", "3"], 0, 2) -&gt; ["3", "2", "1"]</li>
1845    * <li>ArrayUtils.swap(["1", "2", "3"], 0, 0) -&gt; ["1", "2", "3"]</li>
1846    * <li>ArrayUtils.swap(["1", "2", "3"], 1, 0) -&gt; ["2", "1", "3"]</li>
1847    * <li>ArrayUtils.swap(["1", "2", "3"], 0, 5) -&gt; ["1", "2", "3"]</li>
1848    * <li>ArrayUtils.swap(["1", "2", "3"], -1, 1) -&gt; ["2", "1", "3"]</li>
1849    * </ul>
1850    *
1851    * @param array the array to swap, may be {@code null}
1852    * @param offset1 the index of the first element to swap
1853    * @param offset2 the index of the second element to swap
1854    * @since 3.5
1855    */
 
1856  3 toggle public static void swap(final Object[] array, final int offset1, final int offset2) {
1857  3 if (array == null || array.length == 0) {
1858  2 return;
1859    }
1860  1 swap(array, offset1, offset2, 1);
1861    }
1862   
1863    /**
1864    * Swaps two elements in the given long array.
1865    *
1866    * <p>There is no special handling for multi-dimensional arrays. This method
1867    * does nothing for a {@code null} or empty input array or for overflow indices.
1868    * Negative indices are promoted to 0(zero).</p>
1869    *
1870    * Examples:
1871    * <ul>
1872    * <li>ArrayUtils.swap([true, false, true], 0, 2) -&gt; [true, false, true]</li>
1873    * <li>ArrayUtils.swap([true, false, true], 0, 0) -&gt; [true, false, true]</li>
1874    * <li>ArrayUtils.swap([true, false, true], 1, 0) -&gt; [false, true, true]</li>
1875    * <li>ArrayUtils.swap([true, false, true], 0, 5) -&gt; [true, false, true]</li>
1876    * <li>ArrayUtils.swap([true, false, true], -1, 1) -&gt; [false, true, true]</li>
1877    * </ul>
1878    *
1879    *
1880    * @param array the array to swap, may be {@code null}
1881    * @param offset1 the index of the first element to swap
1882    * @param offset2 the index of the second element to swap
1883    * @since 3.5
1884    */
 
1885  4 toggle public static void swap(final long[] array, final int offset1, final int offset2) {
1886  4 if (array == null || array.length == 0) {
1887  2 return;
1888    }
1889  2 swap(array, offset1, offset2, 1);
1890    }
1891   
1892    /**
1893    * Swaps two elements in the given int array.
1894    *
1895    * <p>There is no special handling for multi-dimensional arrays. This method
1896    * does nothing for a {@code null} or empty input array or for overflow indices.
1897    * Negative indices are promoted to 0(zero).</p>
1898    *
1899    * Examples:
1900    * <ul>
1901    * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -&gt; [3, 2, 1]</li>
1902    * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -&gt; [1, 2, 3]</li>
1903    * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -&gt; [2, 1, 3]</li>
1904    * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -&gt; [1, 2, 3]</li>
1905    * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -&gt; [2, 1, 3]</li>
1906    * </ul>
1907    *
1908    * @param array the array to swap, may be {@code null}
1909    * @param offset1 the index of the first element to swap
1910    * @param offset2 the index of the second element to swap
1911    * @since 3.5
1912    */
 
1913  4 toggle public static void swap(final int[] array, final int offset1, final int offset2) {
1914  4 if (array == null || array.length == 0) {
1915  2 return;
1916    }
1917  2 swap(array, offset1, offset2, 1);
1918    }
1919   
1920    /**
1921    * Swaps two elements in the given short array.
1922    *
1923    * <p>There is no special handling for multi-dimensional arrays. This method
1924    * does nothing for a {@code null} or empty input array or for overflow indices.
1925    * Negative indices are promoted to 0(zero).</p>
1926    *
1927    * Examples:
1928    * <ul>
1929    * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -&gt; [3, 2, 1]</li>
1930    * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -&gt; [1, 2, 3]</li>
1931    * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -&gt; [2, 1, 3]</li>
1932    * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -&gt; [1, 2, 3]</li>
1933    * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -&gt; [2, 1, 3]</li>
1934    * </ul>
1935    *
1936    * @param array the array to swap, may be {@code null}
1937    * @param offset1 the index of the first element to swap
1938    * @param offset2 the index of the second element to swap
1939    * @since 3.5
1940    */
 
1941  4 toggle public static void swap(final short[] array, final int offset1, final int offset2) {
1942  4 if (array == null || array.length == 0) {
1943  2 return;
1944    }
1945  2 swap(array, offset1, offset2, 1);
1946    }
1947   
1948    /**
1949    * Swaps two elements in the given char array.
1950    *
1951    * <p>There is no special handling for multi-dimensional arrays. This method
1952    * does nothing for a {@code null} or empty input array or for overflow indices.
1953    * Negative indices are promoted to 0(zero).</p>
1954    *
1955    * Examples:
1956    * <ul>
1957    * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -&gt; [3, 2, 1]</li>
1958    * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -&gt; [1, 2, 3]</li>
1959    * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -&gt; [2, 1, 3]</li>
1960    * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -&gt; [1, 2, 3]</li>
1961    * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -&gt; [2, 1, 3]</li>
1962    * </ul>
1963    *
1964    * @param array the array to swap, may be {@code null}
1965    * @param offset1 the index of the first element to swap
1966    * @param offset2 the index of the second element to swap
1967    * @since 3.5
1968    */
 
1969  6 toggle public static void swap(final char[] array, final int offset1, final int offset2) {
1970  6 if (array == null || array.length == 0) {
1971  2 return;
1972    }
1973  4 swap(array, offset1, offset2, 1);
1974    }
1975   
1976    /**
1977    * Swaps two elements in the given byte array.
1978    *
1979    * <p>There is no special handling for multi-dimensional arrays. This method
1980    * does nothing for a {@code null} or empty input array or for overflow indices.
1981    * Negative indices are promoted to 0(zero).</p>
1982    *
1983    * Examples:
1984    * <ul>
1985    * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -&gt; [3, 2, 1]</li>
1986    * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -&gt; [1, 2, 3]</li>
1987    * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -&gt; [2, 1, 3]</li>
1988    * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -&gt; [1, 2, 3]</li>
1989    * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -&gt; [2, 1, 3]</li>
1990    * </ul>
1991    *
1992    * @param array the array to swap, may be {@code null}
1993    * @param offset1 the index of the first element to swap
1994    * @param offset2 the index of the second element to swap
1995    * @since 3.5
1996    */
 
1997  4 toggle public static void swap(final byte[] array, final int offset1, final int offset2) {
1998  4 if (array == null || array.length == 0) {
1999  2 return;
2000    }
2001  2 swap(array, offset1, offset2, 1);
2002    }
2003   
2004    /**
2005    * Swaps two elements in the given double array.
2006    *
2007    * <p>There is no special handling for multi-dimensional arrays. This method
2008    * does nothing for a {@code null} or empty input array or for overflow indices.
2009    * Negative indices are promoted to 0(zero).</p>
2010    *
2011    * Examples:
2012    * <ul>
2013    * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -&gt; [3, 2, 1]</li>
2014    * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -&gt; [1, 2, 3]</li>
2015    * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -&gt; [2, 1, 3]</li>
2016    * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -&gt; [1, 2, 3]</li>
2017    * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -&gt; [2, 1, 3]</li>
2018    * </ul>
2019    *
2020    * @param array the array to swap, may be {@code null}
2021    * @param offset1 the index of the first element to swap
2022    * @param offset2 the index of the second element to swap
2023    * @since 3.5
2024    */
 
2025  4 toggle public static void swap(final double[] array, final int offset1, final int offset2) {
2026  4 if (array == null || array.length == 0) {
2027  2 return;
2028    }
2029  2 swap(array, offset1, offset2, 1);
2030    }
2031   
2032    /**
2033    * Swaps two elements in the given float array.
2034    *
2035    * <p>There is no special handling for multi-dimensional arrays. This method
2036    * does nothing for a {@code null} or empty input array or for overflow indices.
2037    * Negative indices are promoted to 0(zero).</p>
2038    *
2039    * Examples:
2040    * <ul>
2041    * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -&gt; [3, 2, 1]</li>
2042    * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -&gt; [1, 2, 3]</li>
2043    * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -&gt; [2, 1, 3]</li>
2044    * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -&gt; [1, 2, 3]</li>
2045    * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -&gt; [2, 1, 3]</li>
2046    * </ul>
2047    *
2048    * @param array the array to swap, may be {@code null}
2049    * @param offset1 the index of the first element to swap
2050    * @param offset2 the index of the second element to swap
2051    * @since 3.5
2052    */
 
2053  4 toggle public static void swap(final float[] array, final int offset1, final int offset2) {
2054  4 if (array == null || array.length == 0) {
2055  2 return;
2056    }
2057  2 swap(array, offset1, offset2, 1);
2058    }
2059   
2060    /**
2061    * Swaps two elements in the given boolean array.
2062    *
2063    * <p>There is no special handling for multi-dimensional arrays. This method
2064    * does nothing for a {@code null} or empty input array or for overflow indices.
2065    * Negative indices are promoted to 0(zero).</p>
2066    *
2067    * Examples:
2068    * <ul>
2069    * <li>ArrayUtils.swap([1, 2, 3], 0, 2) -&gt; [3, 2, 1]</li>
2070    * <li>ArrayUtils.swap([1, 2, 3], 0, 0) -&gt; [1, 2, 3]</li>
2071    * <li>ArrayUtils.swap([1, 2, 3], 1, 0) -&gt; [2, 1, 3]</li>
2072    * <li>ArrayUtils.swap([1, 2, 3], 0, 5) -&gt; [1, 2, 3]</li>
2073    * <li>ArrayUtils.swap([1, 2, 3], -1, 1) -&gt; [2, 1, 3]</li>
2074    * </ul>
2075    *
2076    * @param array the array to swap, may be {@code null}
2077    * @param offset1 the index of the first element to swap
2078    * @param offset2 the index of the second element to swap
2079    * @since 3.5
2080    */
 
2081  4 toggle public static void swap(final boolean[] array, final int offset1, final int offset2) {
2082  4 if (array == null || array.length == 0) {
2083  2 return;
2084    }
2085  2 swap(array, offset1, offset2, 1);
2086    }
2087   
2088    /**
2089    * Swaps a series of elements in the given boolean array.
2090    *
2091    * <p>This method does nothing for a {@code null} or empty input array or
2092    * for overflow indices. Negative indices are promoted to 0(zero). If any
2093    * of the sub-arrays to swap falls outside of the given array, then the
2094    * swap is stopped at the end of the array and as many as possible elements
2095    * are swapped.</p>
2096    *
2097    * Examples:
2098    * <ul>
2099    * <li>ArrayUtils.swap([true, false, true, false], 0, 2, 1) -&gt; [true, false, true, false]</li>
2100    * <li>ArrayUtils.swap([true, false, true, false], 0, 0, 1) -&gt; [true, false, true, false]</li>
2101    * <li>ArrayUtils.swap([true, false, true, false], 0, 2, 2) -&gt; [true, false, true, false]</li>
2102    * <li>ArrayUtils.swap([true, false, true, false], -3, 2, 2) -&gt; [true, false, true, false]</li>
2103    * <li>ArrayUtils.swap([true, false, true, false], 0, 3, 3) -&gt; [false, false, true, true]</li>
2104    * </ul>
2105    *
2106    * @param array the array to swap, may be {@code null}
2107    * @param offset1 the index of the first element in the series to swap
2108    * @param offset2 the index of the second element in the series to swap
2109    * @param len the number of elements to swap starting with the given indices
2110    * @since 3.5
2111    */
 
2112  28 toggle public static void swap(final boolean[] array, int offset1, int offset2, int len) {
2113  28 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2114  1 return;
2115    }
2116  27 if (offset1 < 0) {
2117  2 offset1 = 0;
2118    }
2119  27 if (offset2 < 0) {
2120  2 offset2 = 0;
2121    }
2122  27 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2123  57 for (int i = 0; i < len; i++, offset1++, offset2++) {
2124  30 final boolean aux = array[offset1];
2125  30 array[offset1] = array[offset2];
2126  30 array[offset2] = aux;
2127    }
2128    }
2129   
2130    /**
2131    * Swaps a series of elements in the given byte array.
2132    *
2133    * <p>This method does nothing for a {@code null} or empty input array or
2134    * for overflow indices. Negative indices are promoted to 0(zero). If any
2135    * of the sub-arrays to swap falls outside of the given array, then the
2136    * swap is stopped at the end of the array and as many as possible elements
2137    * are swapped.</p>
2138    *
2139    * Examples:
2140    * <ul>
2141    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -&gt; [3, 2, 1, 4]</li>
2142    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -&gt; [1, 2, 3, 4]</li>
2143    * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -&gt; [3, 4, 1, 2]</li>
2144    * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -&gt; [3, 4, 1, 2]</li>
2145    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -&gt; [4, 2, 3, 1]</li>
2146    * </ul>
2147    *
2148    * @param array the array to swap, may be {@code null}
2149    * @param offset1 the index of the first element in the series to swap
2150    * @param offset2 the index of the second element in the series to swap
2151    * @param len the number of elements to swap starting with the given indices
2152    * @since 3.5
2153    */
 
2154  31 toggle public static void swap(final byte[] array, int offset1, int offset2, int len) {
2155  31 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2156  1 return;
2157    }
2158  30 if (offset1 < 0) {
2159  2 offset1 = 0;
2160    }
2161  30 if (offset2 < 0) {
2162  2 offset2 = 0;
2163    }
2164  30 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2165  63 for (int i = 0; i < len; i++, offset1++, offset2++) {
2166  33 final byte aux = array[offset1];
2167  33 array[offset1] = array[offset2];
2168  33 array[offset2] = aux;
2169    }
2170    }
2171   
2172    /**
2173    * Swaps a series of elements in the given char array.
2174    *
2175    * <p>This method does nothing for a {@code null} or empty input array or
2176    * for overflow indices. Negative indices are promoted to 0(zero). If any
2177    * of the sub-arrays to swap falls outside of the given array, then the
2178    * swap is stopped at the end of the array and as many as possible elements
2179    * are swapped.</p>
2180    *
2181    * Examples:
2182    * <ul>
2183    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -&gt; [3, 2, 1, 4]</li>
2184    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -&gt; [1, 2, 3, 4]</li>
2185    * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -&gt; [3, 4, 1, 2]</li>
2186    * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -&gt; [3, 4, 1, 2]</li>
2187    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -&gt; [4, 2, 3, 1]</li>
2188    * </ul>
2189    *
2190    * @param array the array to swap, may be {@code null}
2191    * @param offset1 the index of the first element in the series to swap
2192    * @param offset2 the index of the second element in the series to swap
2193    * @param len the number of elements to swap starting with the given indices
2194    * @since 3.5
2195    */
 
2196  33 toggle public static void swap(final char[] array, int offset1, int offset2, int len) {
2197  33 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2198  1 return;
2199    }
2200  32 if (offset1 < 0) {
2201  2 offset1 = 0;
2202    }
2203  32 if (offset2 < 0) {
2204  2 offset2 = 0;
2205    }
2206  32 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2207  67 for (int i = 0; i < len; i++, offset1++, offset2++) {
2208  35 final char aux = array[offset1];
2209  35 array[offset1] = array[offset2];
2210  35 array[offset2] = aux;
2211    }
2212    }
2213   
2214    /**
2215    * Swaps a series of elements in the given double array.
2216    *
2217    * <p>This method does nothing for a {@code null} or empty input array or
2218    * for overflow indices. Negative indices are promoted to 0(zero). If any
2219    * of the sub-arrays to swap falls outside of the given array, then the
2220    * swap is stopped at the end of the array and as many as possible elements
2221    * are swapped.</p>
2222    *
2223    * Examples:
2224    * <ul>
2225    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -&gt; [3, 2, 1, 4]</li>
2226    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -&gt; [1, 2, 3, 4]</li>
2227    * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -&gt; [3, 4, 1, 2]</li>
2228    * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -&gt; [3, 4, 1, 2]</li>
2229    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -&gt; [4, 2, 3, 1]</li>
2230    * </ul>
2231    *
2232    * @param array the array to swap, may be {@code null}
2233    * @param offset1 the index of the first element in the series to swap
2234    * @param offset2 the index of the second element in the series to swap
2235    * @param len the number of elements to swap starting with the given indices
2236    * @since 3.5
2237    */
 
2238  31 toggle public static void swap(final double[] array, int offset1, int offset2, int len) {
2239  31 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2240  1 return;
2241    }
2242  30 if (offset1 < 0) {
2243  2 offset1 = 0;
2244    }
2245  30 if (offset2 < 0) {
2246  2 offset2 = 0;
2247    }
2248  30 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2249  63 for (int i = 0; i < len; i++, offset1++, offset2++) {
2250  33 final double aux = array[offset1];
2251  33 array[offset1] = array[offset2];
2252  33 array[offset2] = aux;
2253    }
2254    }
2255   
2256    /**
2257    * Swaps a series of elements in the given float array.
2258    *
2259    * <p>This method does nothing for a {@code null} or empty input array or
2260    * for overflow indices. Negative indices are promoted to 0(zero). If any
2261    * of the sub-arrays to swap falls outside of the given array, then the
2262    * swap is stopped at the end of the array and as many as possible elements
2263    * are swapped.</p>
2264    *
2265    * Examples:
2266    * <ul>
2267    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -&gt; [3, 2, 1, 4]</li>
2268    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -&gt; [1, 2, 3, 4]</li>
2269    * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -&gt; [3, 4, 1, 2]</li>
2270    * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -&gt; [3, 4, 1, 2]</li>
2271    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -&gt; [4, 2, 3, 1]</li>
2272    * </ul>
2273    *
2274    * @param array the array to swap, may be {@code null}
2275    * @param offset1 the index of the first element in the series to swap
2276    * @param offset2 the index of the second element in the series to swap
2277    * @param len the number of elements to swap starting with the given indices
2278    * @since 3.5
2279    */
 
2280  31 toggle public static void swap(final float[] array, int offset1, int offset2, int len) {
2281  31 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2282  1 return;
2283    }
2284  30 if (offset1 < 0) {
2285  2 offset1 = 0;
2286    }
2287  30 if (offset2 < 0) {
2288  2 offset2 = 0;
2289    }
2290  30 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2291  63 for (int i = 0; i < len; i++, offset1++, offset2++) {
2292  33 final float aux = array[offset1];
2293  33 array[offset1] = array[offset2];
2294  33 array[offset2] = aux;
2295    }
2296   
2297    }
2298   
2299    /**
2300    * Swaps a series of elements in the given int array.
2301    *
2302    * <p>This method does nothing for a {@code null} or empty input array or
2303    * for overflow indices. Negative indices are promoted to 0(zero). If any
2304    * of the sub-arrays to swap falls outside of the given array, then the
2305    * swap is stopped at the end of the array and as many as possible elements
2306    * are swapped.</p>
2307    *
2308    * Examples:
2309    * <ul>
2310    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -&gt; [3, 2, 1, 4]</li>
2311    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -&gt; [1, 2, 3, 4]</li>
2312    * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -&gt; [3, 4, 1, 2]</li>
2313    * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -&gt; [3, 4, 1, 2]</li>
2314    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -&gt; [4, 2, 3, 1]</li>
2315    * </ul>
2316    *
2317    * @param array the array to swap, may be {@code null}
2318    * @param offset1 the index of the first element in the series to swap
2319    * @param offset2 the index of the second element in the series to swap
2320    * @param len the number of elements to swap starting with the given indices
2321    * @since 3.5
2322    */
 
2323  33 toggle public static void swap(final int[] array, int offset1, int offset2, int len) {
2324  33 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2325  1 return;
2326    }
2327  32 if (offset1 < 0) {
2328  2 offset1 = 0;
2329    }
2330  32 if (offset2 < 0) {
2331  2 offset2 = 0;
2332    }
2333  32 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2334  69 for (int i = 0; i < len; i++, offset1++, offset2++) {
2335  37 final int aux = array[offset1];
2336  37 array[offset1] = array[offset2];
2337  37 array[offset2] = aux;
2338    }
2339    }
2340   
2341    /**
2342    * Swaps a series of elements in the given long array.
2343    *
2344    * <p>This method does nothing for a {@code null} or empty input array or
2345    * for overflow indices. Negative indices are promoted to 0(zero). If any
2346    * of the sub-arrays to swap falls outside of the given array, then the
2347    * swap is stopped at the end of the array and as many as possible elements
2348    * are swapped.</p>
2349    *
2350    * Examples:
2351    * <ul>
2352    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -&gt; [3, 2, 1, 4]</li>
2353    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -&gt; [1, 2, 3, 4]</li>
2354    * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -&gt; [3, 4, 1, 2]</li>
2355    * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -&gt; [3, 4, 1, 2]</li>
2356    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -&gt; [4, 2, 3, 1]</li>
2357    * </ul>
2358    *
2359    * @param array the array to swap, may be {@code null}
2360    * @param offset1 the index of the first element in the series to swap
2361    * @param offset2 the index of the second element in the series to swap
2362    * @param len the number of elements to swap starting with the given indices
2363    * @since 3.5
2364    */
 
2365  31 toggle public static void swap(final long[] array, int offset1, int offset2, int len) {
2366  31 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2367  1 return;
2368    }
2369  30 if (offset1 < 0) {
2370  2 offset1 = 0;
2371    }
2372  30 if (offset2 < 0) {
2373  2 offset2 = 0;
2374    }
2375  30 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2376  63 for (int i = 0; i < len; i++, offset1++, offset2++) {
2377  33 final long aux = array[offset1];
2378  33 array[offset1] = array[offset2];
2379  33 array[offset2] = aux;
2380    }
2381    }
2382   
2383    /**
2384    * Swaps a series of elements in the given array.
2385    *
2386    * <p>This method does nothing for a {@code null} or empty input array or
2387    * for overflow indices. Negative indices are promoted to 0(zero). If any
2388    * of the sub-arrays to swap falls outside of the given array, then the
2389    * swap is stopped at the end of the array and as many as possible elements
2390    * are swapped.</p>
2391    *
2392    * Examples:
2393    * <ul>
2394    * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 2, 1) -&gt; ["3", "2", "1", "4"]</li>
2395    * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 0, 1) -&gt; ["1", "2", "3", "4"]</li>
2396    * <li>ArrayUtils.swap(["1", "2", "3", "4"], 2, 0, 2) -&gt; ["3", "4", "1", "2"]</li>
2397    * <li>ArrayUtils.swap(["1", "2", "3", "4"], -3, 2, 2) -&gt; ["3", "4", "1", "2"]</li>
2398    * <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 3, 3) -&gt; ["4", "2", "3", "1"]</li>
2399    * </ul>
2400    *
2401    * @param array the array to swap, may be {@code null}
2402    * @param offset1 the index of the first element in the series to swap
2403    * @param offset2 the index of the second element in the series to swap
2404    * @param len the number of elements to swap starting with the given indices
2405    * @since 3.5
2406    */
 
2407  31 toggle public static void swap(final Object[] array, int offset1, int offset2, int len) {
2408  31 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2409  2 return;
2410    }
2411  29 if (offset1 < 0) {
2412  2 offset1 = 0;
2413    }
2414  29 if (offset2 < 0) {
2415  1 offset2 = 0;
2416    }
2417  29 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2418  64 for (int i = 0; i < len; i++, offset1++, offset2++) {
2419  35 final Object aux = array[offset1];
2420  35 array[offset1] = array[offset2];
2421  35 array[offset2] = aux;
2422    }
2423    }
2424   
2425    /**
2426    * Swaps a series of elements in the given short array.
2427    *
2428    * <p>This method does nothing for a {@code null} or empty input array or
2429    * for overflow indices. Negative indices are promoted to 0(zero). If any
2430    * of the sub-arrays to swap falls outside of the given array, then the
2431    * swap is stopped at the end of the array and as many as possible elements
2432    * are swapped.</p>
2433    *
2434    * Examples:
2435    * <ul>
2436    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -&gt; [3, 2, 1, 4]</li>
2437    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -&gt; [1, 2, 3, 4]</li>
2438    * <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -&gt; [3, 4, 1, 2]</li>
2439    * <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -&gt; [3, 4, 1, 2]</li>
2440    * <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -&gt; [4, 2, 3, 1]</li>
2441    * </ul>
2442    *
2443    * @param array the array to swap, may be {@code null}
2444    * @param offset1 the index of the first element in the series to swap
2445    * @param offset2 the index of the second element in the series to swap
2446    * @param len the number of elements to swap starting with the given indices
2447    * @since 3.5
2448    */
 
2449  34 toggle public static void swap(final short[] array, int offset1, int offset2, int len) {
2450  34 if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
2451  1 return;
2452    }
2453  33 if (offset1 < 0) {
2454  2 offset1 = 0;
2455    }
2456  33 if (offset2 < 0) {
2457  2 offset2 = 0;
2458    }
2459  33 if (offset1 == offset2) {
2460  4 return;
2461    }
2462  29 len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
2463  60 for (int i = 0; i < len; i++, offset1++, offset2++) {
2464  31 final short aux = array[offset1];
2465  31 array[offset1] = array[offset2];
2466  31 array[offset2] = aux;
2467    }
2468    }
2469   
2470    // Shift
2471    //-----------------------------------------------------------------------
2472    /**
2473    * Shifts the order of the given array.
2474    *
2475    * <p>There is no special handling for multi-dimensional arrays. This method
2476    * does nothing for {@code null} or empty input arrays.</p>
2477    *
2478    * @param array the array to shift, may be {@code null}
2479    * @param offset
2480    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2481    * rotate, than the effective offset is modulo the number of elements to rotate.
2482    * @since 3.5
2483    */
 
2484  7 toggle public static void shift(final Object[] array, final int offset) {
2485  7 if (array == null) {
2486  1 return;
2487    }
2488  6 shift(array, 0, array.length, offset);
2489    }
2490   
2491    /**
2492    * Shifts the order of the given long array.
2493    *
2494    * <p>There is no special handling for multi-dimensional arrays. This method
2495    * does nothing for {@code null} or empty input arrays.</p>
2496    *
2497    * @param array the array to shift, may be {@code null}
2498    * @param offset
2499    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2500    * rotate, than the effective offset is modulo the number of elements to rotate.
2501    * @since 3.5
2502    */
 
2503  7 toggle public static void shift(final long[] array, final int offset) {
2504  7 if (array == null) {
2505  1 return;
2506    }
2507  6 shift(array, 0, array.length, offset);
2508    }
2509   
2510    /**
2511    * Shifts the order of the given int array.
2512    *
2513    * <p>There is no special handling for multi-dimensional arrays. This method
2514    * does nothing for {@code null} or empty input arrays.</p>
2515    *
2516    * @param array the array to shift, may be {@code null}
2517    * @param offset
2518    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2519    * rotate, than the effective offset is modulo the number of elements to rotate.
2520    * @since 3.5
2521    */
 
2522  7 toggle public static void shift(final int[] array, final int offset) {
2523  7 if (array == null) {
2524  1 return;
2525    }
2526  6 shift(array, 0, array.length, offset);
2527    }
2528   
2529    /**
2530    * Shifts the order of the given short array.
2531    *
2532    * <p>There is no special handling for multi-dimensional arrays. This method
2533    * does nothing for {@code null} or empty input arrays.</p>
2534    *
2535    * @param array the array to shift, may be {@code null}
2536    * @param offset
2537    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2538    * rotate, than the effective offset is modulo the number of elements to rotate.
2539    * @since 3.5
2540    */
 
2541  8 toggle public static void shift(final short[] array, final int offset) {
2542  8 if (array == null) {
2543  1 return;
2544    }
2545  7 shift(array, 0, array.length, offset);
2546    }
2547   
2548    /**
2549    * Shifts the order of the given char array.
2550    *
2551    * <p>There is no special handling for multi-dimensional arrays. This method
2552    * does nothing for {@code null} or empty input arrays.</p>
2553    *
2554    * @param array the array to shift, may be {@code null}
2555    * @param offset
2556    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2557    * rotate, than the effective offset is modulo the number of elements to rotate.
2558    * @since 3.5
2559    */
 
2560  6 toggle public static void shift(final char[] array, final int offset) {
2561  6 if (array == null) {
2562  0 return;
2563    }
2564  6 shift(array, 0, array.length, offset);
2565    }
2566   
2567    /**
2568    * Shifts the order of the given byte array.
2569    *
2570    * <p>There is no special handling for multi-dimensional arrays. This method
2571    * does nothing for {@code null} or empty input arrays.</p>
2572    *
2573    * @param array the array to shift, may be {@code null}
2574    * @param offset
2575    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2576    * rotate, than the effective offset is modulo the number of elements to rotate.
2577    * @since 3.5
2578    */
 
2579  6 toggle public static void shift(final byte[] array, final int offset) {
2580  6 if (array == null) {
2581  0 return;
2582    }
2583  6 shift(array, 0, array.length, offset);
2584    }
2585   
2586    /**
2587    * Shifts the order of the given double array.
2588    *
2589    * <p>There is no special handling for multi-dimensional arrays. This method
2590    * does nothing for {@code null} or empty input arrays.</p>
2591    *
2592    * @param array the array to shift, may be {@code null}
2593    * @param offset
2594    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2595    * rotate, than the effective offset is modulo the number of elements to rotate.
2596    * @since 3.5
2597    */
 
2598  7 toggle public static void shift(final double[] array, final int offset) {
2599  7 if (array == null) {
2600  1 return;
2601    }
2602  6 shift(array, 0, array.length, offset);
2603    }
2604   
2605    /**
2606    * Shifts the order of the given float array.
2607    *
2608    * <p>There is no special handling for multi-dimensional arrays. This method
2609    * does nothing for {@code null} or empty input arrays.</p>
2610    *
2611    * @param array the array to shift, may be {@code null}
2612    * @param offset
2613    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2614    * rotate, than the effective offset is modulo the number of elements to rotate.
2615    * @since 3.5
2616    */
 
2617  7 toggle public static void shift(final float[] array, final int offset) {
2618  7 if (array == null) {
2619  1 return;
2620    }
2621  6 shift(array, 0, array.length, offset);
2622    }
2623   
2624    /**
2625    * Shifts the order of the given boolean array.
2626    *
2627    * <p>There is no special handling for multi-dimensional arrays. This method
2628    * does nothing for {@code null} or empty input arrays.</p>
2629    *
2630    * @param array the array to shift, may be {@code null}
2631    * @param offset
2632    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2633    * rotate, than the effective offset is modulo the number of elements to rotate.
2634    * @since 3.5
2635    */
 
2636  5 toggle public static void shift(final boolean[] array, final int offset) {
2637  5 if (array == null) {
2638  1 return;
2639    }
2640  4 shift(array, 0, array.length, offset);
2641    }
2642   
2643    /**
2644    * Shifts the order of a series of elements in the given boolean array.
2645    *
2646    * <p>There is no special handling for multi-dimensional arrays. This method
2647    * does nothing for {@code null} or empty input arrays.</p>
2648    *
2649    * @param array
2650    * the array to shift, may be {@code null}
2651    * @param startIndexInclusive
2652    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
2653    * change.
2654    * @param endIndexExclusive
2655    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
2656    * change. Overvalue (&gt;array.length) is demoted to array length.
2657    * @param offset
2658    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2659    * rotate, than the effective offset is modulo the number of elements to rotate.
2660    * @since 3.5
2661    */
 
2662  4 toggle public static void shift(final boolean[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
2663  4 if (array == null) {
2664  0 return;
2665    }
2666  4 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
2667  0 return;
2668    }
2669  4 if (startIndexInclusive < 0) {
2670  0 startIndexInclusive = 0;
2671    }
2672  4 if (endIndexExclusive >= array.length) {
2673  4 endIndexExclusive = array.length;
2674    }
2675  4 int n = endIndexExclusive - startIndexInclusive;
2676  4 if (n <= 1) {
2677  0 return;
2678    }
2679  4 offset %= n;
2680  4 if (offset < 0) {
2681  2 offset += n;
2682    }
2683    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
2684    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
2685  12 while (n > 1 && offset > 0) {
2686  12 final int n_offset = n - offset;
2687   
2688  12 if (offset > n_offset) {
2689  2 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
2690  2 n = offset;
2691  2 offset -= n_offset;
2692  10 } else if (offset < n_offset) {
2693  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2694  6 startIndexInclusive += offset;
2695  6 n = n_offset;
2696    } else {
2697  4 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2698  4 break;
2699    }
2700    }
2701    }
2702   
2703    /**
2704    * Shifts the order of a series of elements in the given byte array.
2705    *
2706    * <p>There is no special handling for multi-dimensional arrays. This method
2707    * does nothing for {@code null} or empty input arrays.</p>
2708    *
2709    * @param array
2710    * the array to shift, may be {@code null}
2711    * @param startIndexInclusive
2712    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
2713    * change.
2714    * @param endIndexExclusive
2715    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
2716    * change. Overvalue (&gt;array.length) is demoted to array length.
2717    * @param offset
2718    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2719    * rotate, than the effective offset is modulo the number of elements to rotate.
2720    * @since 3.5
2721    */
 
2722  10 toggle public static void shift(final byte[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
2723  10 if (array == null) {
2724  1 return;
2725    }
2726  9 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
2727  0 return;
2728    }
2729  9 if (startIndexInclusive < 0) {
2730  0 startIndexInclusive = 0;
2731    }
2732  9 if (endIndexExclusive >= array.length) {
2733  6 endIndexExclusive = array.length;
2734    }
2735  9 int n = endIndexExclusive - startIndexInclusive;
2736  9 if (n <= 1) {
2737  1 return;
2738    }
2739  8 offset %= n;
2740  8 if (offset < 0) {
2741  2 offset += n;
2742    }
2743    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
2744    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
2745  17 while (n > 1 && offset > 0) {
2746  15 final int n_offset = n - offset;
2747   
2748  15 if (offset > n_offset) {
2749  3 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
2750  3 n = offset;
2751  3 offset -= n_offset;
2752  12 } else if (offset < n_offset) {
2753  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2754  6 startIndexInclusive += offset;
2755  6 n = n_offset;
2756    } else {
2757  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2758  6 break;
2759    }
2760    }
2761    }
2762   
2763    /**
2764    * Shifts the order of a series of elements in the given char array.
2765    *
2766    * <p>There is no special handling for multi-dimensional arrays. This method
2767    * does nothing for {@code null} or empty input arrays.</p>
2768    *
2769    * @param array
2770    * the array to shift, may be {@code null}
2771    * @param startIndexInclusive
2772    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
2773    * change.
2774    * @param endIndexExclusive
2775    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
2776    * change. Overvalue (&gt;array.length) is demoted to array length.
2777    * @param offset
2778    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2779    * rotate, than the effective offset is modulo the number of elements to rotate.
2780    * @since 3.5
2781    */
 
2782  10 toggle public static void shift(final char[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
2783  10 if (array == null) {
2784  1 return;
2785    }
2786  9 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
2787  0 return;
2788    }
2789  9 if (startIndexInclusive < 0) {
2790  0 startIndexInclusive = 0;
2791    }
2792  9 if (endIndexExclusive >= array.length) {
2793  6 endIndexExclusive = array.length;
2794    }
2795  9 int n = endIndexExclusive - startIndexInclusive;
2796  9 if (n <= 1) {
2797  1 return;
2798    }
2799  8 offset %= n;
2800  8 if (offset < 0) {
2801  2 offset += n;
2802    }
2803    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
2804    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
2805  17 while (n > 1 && offset > 0) {
2806  15 final int n_offset = n - offset;
2807   
2808  15 if (offset > n_offset) {
2809  3 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
2810  3 n = offset;
2811  3 offset -= n_offset;
2812  12 } else if (offset < n_offset) {
2813  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2814  6 startIndexInclusive += offset;
2815  6 n = n_offset;
2816    } else {
2817  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2818  6 break;
2819    }
2820    }
2821    }
2822   
2823    /**
2824    * Shifts the order of a series of elements in the given double array.
2825    *
2826    * <p>There is no special handling for multi-dimensional arrays. This method
2827    * does nothing for {@code null} or empty input arrays.</p>
2828    *
2829    * @param array
2830    * the array to shift, may be {@code null}
2831    * @param startIndexInclusive
2832    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
2833    * change.
2834    * @param endIndexExclusive
2835    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
2836    * change. Overvalue (&gt;array.length) is demoted to array length.
2837    * @param offset
2838    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2839    * rotate, than the effective offset is modulo the number of elements to rotate.
2840    * @since 3.5
2841    */
 
2842  10 toggle public static void shift(final double[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
2843  10 if (array == null) {
2844  1 return;
2845    }
2846  9 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
2847  0 return;
2848    }
2849  9 if (startIndexInclusive < 0) {
2850  0 startIndexInclusive = 0;
2851    }
2852  9 if (endIndexExclusive >= array.length) {
2853  6 endIndexExclusive = array.length;
2854    }
2855  9 int n = endIndexExclusive - startIndexInclusive;
2856  9 if (n <= 1) {
2857  1 return;
2858    }
2859  8 offset %= n;
2860  8 if (offset < 0) {
2861  2 offset += n;
2862    }
2863    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
2864    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
2865  17 while (n > 1 && offset > 0) {
2866  15 final int n_offset = n - offset;
2867   
2868  15 if (offset > n_offset) {
2869  3 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
2870  3 n = offset;
2871  3 offset -= n_offset;
2872  12 } else if (offset < n_offset) {
2873  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2874  6 startIndexInclusive += offset;
2875  6 n = n_offset;
2876    } else {
2877  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2878  6 break;
2879    }
2880    }
2881    }
2882   
2883    /**
2884    * Shifts the order of a series of elements in the given float array.
2885    *
2886    * <p>There is no special handling for multi-dimensional arrays. This method
2887    * does nothing for {@code null} or empty input arrays.</p>
2888    *
2889    * @param array
2890    * the array to shift, may be {@code null}
2891    * @param startIndexInclusive
2892    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
2893    * change.
2894    * @param endIndexExclusive
2895    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
2896    * change. Overvalue (&gt;array.length) is demoted to array length.
2897    * @param offset
2898    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2899    * rotate, than the effective offset is modulo the number of elements to rotate.
2900    * @since 3.5
2901    */
 
2902  10 toggle public static void shift(final float[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
2903  10 if (array == null) {
2904  1 return;
2905    }
2906  9 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
2907  0 return;
2908    }
2909  9 if (startIndexInclusive < 0) {
2910  0 startIndexInclusive = 0;
2911    }
2912  9 if (endIndexExclusive >= array.length) {
2913  6 endIndexExclusive = array.length;
2914    }
2915  9 int n = endIndexExclusive - startIndexInclusive;
2916  9 if (n <= 1) {
2917  1 return;
2918    }
2919  8 offset %= n;
2920  8 if (offset < 0) {
2921  2 offset += n;
2922    }
2923    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
2924    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
2925  17 while (n > 1 && offset > 0) {
2926  15 final int n_offset = n - offset;
2927   
2928  15 if (offset > n_offset) {
2929  3 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
2930  3 n = offset;
2931  3 offset -= n_offset;
2932  12 } else if (offset < n_offset) {
2933  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2934  6 startIndexInclusive += offset;
2935  6 n = n_offset;
2936    } else {
2937  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2938  6 break;
2939    }
2940    }
2941    }
2942   
2943    /**
2944    * Shifts the order of a series of elements in the given int array.
2945    *
2946    * <p>There is no special handling for multi-dimensional arrays. This method
2947    * does nothing for {@code null} or empty input arrays.</p>
2948    *
2949    * @param array
2950    * the array to shift, may be {@code null}
2951    * @param startIndexInclusive
2952    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
2953    * change.
2954    * @param endIndexExclusive
2955    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
2956    * change. Overvalue (&gt;array.length) is demoted to array length.
2957    * @param offset
2958    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
2959    * rotate, than the effective offset is modulo the number of elements to rotate.
2960    * @since 3.5
2961    */
 
2962  10 toggle public static void shift(final int[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
2963  10 if (array == null) {
2964  1 return;
2965    }
2966  9 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
2967  0 return;
2968    }
2969  9 if (startIndexInclusive < 0) {
2970  0 startIndexInclusive = 0;
2971    }
2972  9 if (endIndexExclusive >= array.length) {
2973  6 endIndexExclusive = array.length;
2974    }
2975  9 int n = endIndexExclusive - startIndexInclusive;
2976  9 if (n <= 1) {
2977  1 return;
2978    }
2979  8 offset %= n;
2980  8 if (offset < 0) {
2981  2 offset += n;
2982    }
2983    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
2984    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
2985  17 while (n > 1 && offset > 0) {
2986  15 final int n_offset = n - offset;
2987   
2988  15 if (offset > n_offset) {
2989  3 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
2990  3 n = offset;
2991  3 offset -= n_offset;
2992  12 } else if (offset < n_offset) {
2993  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2994  6 startIndexInclusive += offset;
2995  6 n = n_offset;
2996    } else {
2997  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
2998  6 break;
2999    }
3000    }
3001    }
3002   
3003    /**
3004    * Shifts the order of a series of elements in the given long array.
3005    *
3006    * <p>There is no special handling for multi-dimensional arrays. This method
3007    * does nothing for {@code null} or empty input arrays.</p>
3008    *
3009    * @param array
3010    * the array to shift, may be {@code null}
3011    * @param startIndexInclusive
3012    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
3013    * change.
3014    * @param endIndexExclusive
3015    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
3016    * change. Overvalue (&gt;array.length) is demoted to array length.
3017    * @param offset
3018    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
3019    * rotate, than the effective offset is modulo the number of elements to rotate.
3020    * @since 3.5
3021    */
 
3022  10 toggle public static void shift(final long[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
3023  10 if (array == null) {
3024  1 return;
3025    }
3026  9 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
3027  0 return;
3028    }
3029  9 if (startIndexInclusive < 0) {
3030  0 startIndexInclusive = 0;
3031    }
3032  9 if (endIndexExclusive >= array.length) {
3033  6 endIndexExclusive = array.length;
3034    }
3035  9 int n = endIndexExclusive - startIndexInclusive;
3036  9 if (n <= 1) {
3037  1 return;
3038    }
3039  8 offset %= n;
3040  8 if (offset < 0) {
3041  2 offset += n;
3042    }
3043    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
3044    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
3045  17 while (n > 1 && offset > 0) {
3046  15 final int n_offset = n - offset;
3047   
3048  15 if (offset > n_offset) {
3049  3 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
3050  3 n = offset;
3051  3 offset -= n_offset;
3052  12 } else if (offset < n_offset) {
3053  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
3054  6 startIndexInclusive += offset;
3055  6 n = n_offset;
3056    } else {
3057  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
3058  6 break;
3059    }
3060    }
3061    }
3062   
3063    /**
3064    * Shifts the order of a series of elements in the given array.
3065    *
3066    * <p>There is no special handling for multi-dimensional arrays. This method
3067    * does nothing for {@code null} or empty input arrays.</p>
3068    *
3069    * @param array
3070    * the array to shift, may be {@code null}
3071    * @param startIndexInclusive
3072    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
3073    * change.
3074    * @param endIndexExclusive
3075    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
3076    * change. Overvalue (&gt;array.length) is demoted to array length.
3077    * @param offset
3078    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
3079    * rotate, than the effective offset is modulo the number of elements to rotate.
3080    * @since 3.5
3081    */
 
3082  10 toggle public static void shift(final Object[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
3083  10 if (array == null) {
3084  1 return;
3085    }
3086  9 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
3087  0 return;
3088    }
3089  9 if (startIndexInclusive < 0) {
3090  0 startIndexInclusive = 0;
3091    }
3092  9 if (endIndexExclusive >= array.length) {
3093  6 endIndexExclusive = array.length;
3094    }
3095  9 int n = endIndexExclusive - startIndexInclusive;
3096  9 if (n <= 1) {
3097  1 return;
3098    }
3099  8 offset %= n;
3100  8 if (offset < 0) {
3101  2 offset += n;
3102    }
3103    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
3104    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
3105  17 while (n > 1 && offset > 0) {
3106  15 final int n_offset = n - offset;
3107   
3108  15 if (offset > n_offset) {
3109  3 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
3110  3 n = offset;
3111  3 offset -= n_offset;
3112  12 } else if (offset < n_offset) {
3113  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
3114  6 startIndexInclusive += offset;
3115  6 n = n_offset;
3116    } else {
3117  6 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
3118  6 break;
3119    }
3120    }
3121    }
3122   
3123    /**
3124    * Shifts the order of a series of elements in the given short array.
3125    *
3126    * <p>There is no special handling for multi-dimensional arrays. This method
3127    * does nothing for {@code null} or empty input arrays.</p>
3128    *
3129    * @param array
3130    * the array to shift, may be {@code null}
3131    * @param startIndexInclusive
3132    * the starting index. Undervalue (&lt;0) is promoted to 0, overvalue (&gt;array.length) results in no
3133    * change.
3134    * @param endIndexExclusive
3135    * elements up to endIndex-1 are shifted in the array. Undervalue (&lt; start index) results in no
3136    * change. Overvalue (&gt;array.length) is demoted to array length.
3137    * @param offset
3138    * The number of positions to rotate the elements. If the offset is larger than the number of elements to
3139    * rotate, than the effective offset is modulo the number of elements to rotate.
3140    * @since 3.5
3141    */
 
3142  11 toggle public static void shift(final short[] array, int startIndexInclusive, int endIndexExclusive, int offset) {
3143  11 if (array == null) {
3144  1 return;
3145    }
3146  10 if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) {
3147  0 return;
3148    }
3149  10 if (startIndexInclusive < 0) {
3150  0 startIndexInclusive = 0;
3151    }
3152  10 if (endIndexExclusive >= array.length) {
3153  7 endIndexExclusive = array.length;
3154    }
3155  10 int n = endIndexExclusive - startIndexInclusive;
3156  10 if (n <= 1) {
3157  1 return;
3158    }
3159  9 offset %= n;
3160  9 if (offset < 0) {
3161  2 offset += n;
3162    }
3163    // For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
3164    // see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
3165  20 while (n > 1 && offset > 0) {
3166  18 final int n_offset = n - offset;
3167   
3168  18 if (offset > n_offset) {
3169  4 swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset);
3170  4 n = offset;
3171  4 offset -= n_offset;
3172  14 } else if (offset < n_offset) {
3173  7 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
3174  7 startIndexInclusive += offset;
3175  7 n = n_offset;
3176    } else {
3177  7 swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset);
3178  7 break;
3179    }
3180    }
3181    }
3182   
3183    // IndexOf search
3184    // ----------------------------------------------------------------------
3185   
3186    // Object IndexOf
3187    //-----------------------------------------------------------------------
3188    /**
3189    * <p>Finds the index of the given object in the array.
3190    *
3191    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3192    *
3193    * @param array the array to search through for the object, may be {@code null}
3194    * @param objectToFind the object to find, may be {@code null}
3195    * @return the index of the object within the array,
3196    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3197    */
 
3198  1412 toggle public static int indexOf(final Object[] array, final Object objectToFind) {
3199  1412 return indexOf(array, objectToFind, 0);
3200    }
3201   
3202    /**
3203    * <p>Finds the index of the given object in the array starting at the given index.
3204    *
3205    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3206    *
3207    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3208    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3209    *
3210    * @param array the array to search through for the object, may be {@code null}
3211    * @param objectToFind the object to find, may be {@code null}
3212    * @param startIndex the index to start searching at
3213    * @return the index of the object within the array starting at the index,
3214    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3215    */
 
3216  1430 toggle public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
3217  1430 if (array == null) {
3218  98 return INDEX_NOT_FOUND;
3219    }
3220  1332 if (startIndex < 0) {
3221  1 startIndex = 0;
3222    }
3223  1332 if (objectToFind == null) {
3224  19 for (int i = startIndex; i < array.length; i++) {
3225  18 if (array[i] == null) {
3226  4 return i;
3227    }
3228    }
3229    } else {
3230  1509 for (int i = startIndex; i < array.length; i++) {
3231  316 if (objectToFind.equals(array[i])) {
3232  134 return i;
3233    }
3234    }
3235    }
3236  1194 return INDEX_NOT_FOUND;
3237    }
3238   
3239    /**
3240    * <p>Finds the last index of the given object within the array.
3241    *
3242    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3243    *
3244    * @param array the array to traverse backwards looking for the object, may be {@code null}
3245    * @param objectToFind the object to find, may be {@code null}
3246    * @return the last index of the object within the array,
3247    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3248    */
 
3249  8 toggle public static int lastIndexOf(final Object[] array, final Object objectToFind) {
3250  8 return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
3251    }
3252   
3253    /**
3254    * <p>Finds the last index of the given object in the array starting at the given index.
3255    *
3256    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3257    *
3258    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
3259    * the array length will search from the end of the array.
3260    *
3261    * @param array the array to traverse for looking for the object, may be {@code null}
3262    * @param objectToFind the object to find, may be {@code null}
3263    * @param startIndex the start index to traverse backwards from
3264    * @return the last index of the object within the array,
3265    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3266    */
 
3267  20 toggle public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) {
3268  20 if (array == null) {
3269  4 return INDEX_NOT_FOUND;
3270    }
3271  16 if (startIndex < 0) {
3272  2 return INDEX_NOT_FOUND;
3273  14 } else if (startIndex >= array.length) {
3274  7 startIndex = array.length - 1;
3275    }
3276  14 if (objectToFind == null) {
3277  8 for (int i = startIndex; i >= 0; i--) {
3278  7 if (array[i] == null) {
3279  2 return i;
3280    }
3281    }
3282  11 } else if (array.getClass().getComponentType().isInstance(objectToFind)) {
3283  38 for (int i = startIndex; i >= 0; i--) {
3284  35 if (objectToFind.equals(array[i])) {
3285  8 return i;
3286    }
3287    }
3288    }
3289  4 return INDEX_NOT_FOUND;
3290    }
3291   
3292    /**
3293    * <p>Checks if the object is in the given array.
3294    *
3295    * <p>The method returns {@code false} if a {@code null} array is passed in.
3296    *
3297    * @param array the array to search through
3298    * @param objectToFind the object to find
3299    * @return {@code true} if the array contains the object
3300    */
 
3301  1392 toggle public static boolean contains(final Object[] array, final Object objectToFind) {
3302  1392 return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
3303    }
3304   
3305    // long IndexOf
3306    //-----------------------------------------------------------------------
3307    /**
3308    * <p>Finds the index of the given value in the array.
3309    *
3310    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3311    *
3312    * @param array the array to search through for the object, may be {@code null}
3313    * @param valueToFind the value to find
3314    * @return the index of the value within the array,
3315    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3316    */
 
3317  33 toggle public static int indexOf(final long[] array, final long valueToFind) {
3318  33 return indexOf(array, valueToFind, 0);
3319    }
3320   
3321    /**
3322    * <p>Finds the index of the given value in the array starting at the given index.
3323    *
3324    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3325    *
3326    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3327    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3328    *
3329    * @param array the array to search through for the object, may be {@code null}
3330    * @param valueToFind the value to find
3331    * @param startIndex the index to start searching at
3332    * @return the index of the value within the array,
3333    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3334    */
 
3335  47 toggle public static int indexOf(final long[] array, final long valueToFind, int startIndex) {
3336  47 if (array == null) {
3337  5 return INDEX_NOT_FOUND;
3338    }
3339  42 if (startIndex < 0) {
3340  1 startIndex = 0;
3341    }
3342  130 for (int i = startIndex; i < array.length; i++) {
3343  119 if (valueToFind == array[i]) {
3344  31 return i;
3345    }
3346    }
3347  11 return INDEX_NOT_FOUND;
3348    }
3349   
3350    /**
3351    * <p>Finds the last index of the given value within the array.
3352    *
3353    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3354    *
3355    * @param array the array to traverse backwards looking for the object, may be {@code null}
3356    * @param valueToFind the object to find
3357    * @return the last index of the value within the array,
3358    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3359    */
 
3360  6 toggle public static int lastIndexOf(final long[] array, final long valueToFind) {
3361  6 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
3362    }
3363   
3364    /**
3365    * <p>Finds the last index of the given value in the array starting at the given index.
3366    *
3367    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3368    *
3369    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
3370    * array length will search from the end of the array.
3371    *
3372    * @param array the array to traverse for looking for the object, may be {@code null}
3373    * @param valueToFind the value to find
3374    * @param startIndex the start index to traverse backwards from
3375    * @return the last index of the value within the array,
3376    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3377    */
 
3378  14 toggle public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) {
3379  14 if (array == null) {
3380  2 return INDEX_NOT_FOUND;
3381    }
3382  12 if (startIndex < 0) {
3383  1 return INDEX_NOT_FOUND;
3384  11 } else if (startIndex >= array.length) {
3385  6 startIndex = array.length - 1;
3386    }
3387  33 for (int i = startIndex; i >= 0; i--) {
3388  30 if (valueToFind == array[i]) {
3389  8 return i;
3390    }
3391    }
3392  3 return INDEX_NOT_FOUND;
3393    }
3394   
3395    /**
3396    * <p>Checks if the value is in the given array.
3397    *
3398    * <p>The method returns {@code false} if a {@code null} array is passed in.
3399    *
3400    * @param array the array to search through
3401    * @param valueToFind the value to find
3402    * @return {@code true} if the array contains the object
3403    */
 
3404  16 toggle public static boolean contains(final long[] array, final long valueToFind) {
3405  16 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
3406    }
3407   
3408    // int IndexOf
3409    //-----------------------------------------------------------------------
3410    /**
3411    * <p>Finds the index of the given value in the array.
3412    *
3413    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3414    *
3415    * @param array the array to search through for the object, may be {@code null}
3416    * @param valueToFind the value to find
3417    * @return the index of the value within the array,
3418    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3419    */
 
3420  33 toggle public static int indexOf(final int[] array, final int valueToFind) {
3421  33 return indexOf(array, valueToFind, 0);
3422    }
3423   
3424    /**
3425    * <p>Finds the index of the given value in the array starting at the given index.
3426    *
3427    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3428    *
3429    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3430    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3431    *
3432    * @param array the array to search through for the object, may be {@code null}
3433    * @param valueToFind the value to find
3434    * @param startIndex the index to start searching at
3435    * @return the index of the value within the array,
3436    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3437    */
 
3438  47 toggle public static int indexOf(final int[] array, final int valueToFind, int startIndex) {
3439  47 if (array == null) {
3440  5 return INDEX_NOT_FOUND;
3441    }
3442  42 if (startIndex < 0) {
3443  1 startIndex = 0;
3444    }
3445  130 for (int i = startIndex; i < array.length; i++) {
3446  119 if (valueToFind == array[i]) {
3447  31 return i;
3448    }
3449    }
3450  11 return INDEX_NOT_FOUND;
3451    }
3452   
3453    /**
3454    * <p>Finds the last index of the given value within the array.
3455    *
3456    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3457    *
3458    * @param array the array to traverse backwards looking for the object, may be {@code null}
3459    * @param valueToFind the object to find
3460    * @return the last index of the value within the array,
3461    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3462    */
 
3463  7 toggle public static int lastIndexOf(final int[] array, final int valueToFind) {
3464  7 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
3465    }
3466   
3467    /**
3468    * <p>Finds the last index of the given value in the array starting at the given index.
3469    *
3470    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3471    *
3472    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
3473    * array length will search from the end of the array.
3474    *
3475    * @param array the array to traverse for looking for the object, may be {@code null}
3476    * @param valueToFind the value to find
3477    * @param startIndex the start index to traverse backwards from
3478    * @return the last index of the value within the array,
3479    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3480    */
 
3481  14 toggle public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) {
3482  14 if (array == null) {
3483  2 return INDEX_NOT_FOUND;
3484    }
3485  12 if (startIndex < 0) {
3486  1 return INDEX_NOT_FOUND;
3487  11 } else if (startIndex >= array.length) {
3488  7 startIndex = array.length - 1;
3489    }
3490  33 for (int i = startIndex; i >= 0; i--) {
3491  30 if (valueToFind == array[i]) {
3492  8 return i;
3493    }
3494    }
3495  3 return INDEX_NOT_FOUND;
3496    }
3497   
3498    /**
3499    * <p>Checks if the value is in the given array.
3500    *
3501    * <p>The method returns {@code false} if a {@code null} array is passed in.
3502    *
3503    * @param array the array to search through
3504    * @param valueToFind the value to find
3505    * @return {@code true} if the array contains the object
3506    */
 
3507  16 toggle public static boolean contains(final int[] array, final int valueToFind) {
3508  16 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
3509    }
3510   
3511    // short IndexOf
3512    //-----------------------------------------------------------------------
3513    /**
3514    * <p>Finds the index of the given value in the array.
3515    *
3516    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3517    *
3518    * @param array the array to search through for the object, may be {@code null}
3519    * @param valueToFind the value to find
3520    * @return the index of the value within the array,
3521    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3522    */
 
3523  33 toggle public static int indexOf(final short[] array, final short valueToFind) {
3524  33 return indexOf(array, valueToFind, 0);
3525    }
3526   
3527    /**
3528    * <p>Finds the index of the given value in the array starting at the given index.
3529    *
3530    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3531    *
3532    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3533    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3534    *
3535    * @param array the array to search through for the object, may be {@code null}
3536    * @param valueToFind the value to find
3537    * @param startIndex the index to start searching at
3538    * @return the index of the value within the array,
3539    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3540    */
 
3541  47 toggle public static int indexOf(final short[] array, final short valueToFind, int startIndex) {
3542  47 if (array == null) {
3543  5 return INDEX_NOT_FOUND;
3544    }
3545  42 if (startIndex < 0) {
3546  1 startIndex = 0;
3547    }
3548  130 for (int i = startIndex; i < array.length; i++) {
3549  119 if (valueToFind == array[i]) {
3550  31 return i;
3551    }
3552    }
3553  11 return INDEX_NOT_FOUND;
3554    }
3555   
3556    /**
3557    * <p>Finds the last index of the given value within the array.
3558    *
3559    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3560    *
3561    * @param array the array to traverse backwards looking for the object, may be {@code null}
3562    * @param valueToFind the object to find
3563    * @return the last index of the value within the array,
3564    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3565    */
 
3566  7 toggle public static int lastIndexOf(final short[] array, final short valueToFind) {
3567  7 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
3568    }
3569   
3570    /**
3571    * <p>Finds the last index of the given value in the array starting at the given index.
3572    *
3573    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3574    *
3575    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
3576    * array length will search from the end of the array.
3577    *
3578    * @param array the array to traverse for looking for the object, may be {@code null}
3579    * @param valueToFind the value to find
3580    * @param startIndex the start index to traverse backwards from
3581    * @return the last index of the value within the array,
3582    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3583    */
 
3584  14 toggle public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) {
3585  14 if (array == null) {
3586  2 return INDEX_NOT_FOUND;
3587    }
3588  12 if (startIndex < 0) {
3589  1 return INDEX_NOT_FOUND;
3590  11 } else if (startIndex >= array.length) {
3591  7 startIndex = array.length - 1;
3592    }
3593  33 for (int i = startIndex; i >= 0; i--) {
3594  30 if (valueToFind == array[i]) {
3595  8 return i;
3596    }
3597    }
3598  3 return INDEX_NOT_FOUND;
3599    }
3600   
3601    /**
3602    * <p>Checks if the value is in the given array.
3603    *
3604    * <p>The method returns {@code false} if a {@code null} array is passed in.
3605    *
3606    * @param array the array to search through
3607    * @param valueToFind the value to find
3608    * @return {@code true} if the array contains the object
3609    */
 
3610  16 toggle public static boolean contains(final short[] array, final short valueToFind) {
3611  16 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
3612    }
3613   
3614    // char IndexOf
3615    //-----------------------------------------------------------------------
3616    /**
3617    * <p>Finds the index of the given value in the array.
3618    *
3619    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3620    *
3621    * @param array the array to search through for the object, may be {@code null}
3622    * @param valueToFind the value to find
3623    * @return the index of the value within the array,
3624    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3625    * @since 2.1
3626    */
 
3627  33 toggle public static int indexOf(final char[] array, final char valueToFind) {
3628  33 return indexOf(array, valueToFind, 0);
3629    }
3630   
3631    /**
3632    * <p>Finds the index of the given value in the array starting at the given index.
3633    *
3634    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3635    *
3636    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3637    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3638    *
3639    * @param array the array to search through for the object, may be {@code null}
3640    * @param valueToFind the value to find
3641    * @param startIndex the index to start searching at
3642    * @return the index of the value within the array,
3643    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3644    * @since 2.1
3645    */
 
3646  47 toggle public static int indexOf(final char[] array, final char valueToFind, int startIndex) {
3647  47 if (array == null) {
3648  5 return INDEX_NOT_FOUND;
3649    }
3650  42 if (startIndex < 0) {
3651  1 startIndex = 0;
3652    }
3653  130 for (int i = startIndex; i < array.length; i++) {
3654  119 if (valueToFind == array[i]) {
3655  31 return i;
3656    }
3657    }
3658  11 return INDEX_NOT_FOUND;
3659    }
3660   
3661    /**
3662    * <p>Finds the last index of the given value within the array.
3663    *
3664    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3665    *
3666    * @param array the array to traverse backwards looking for the object, may be {@code null}
3667    * @param valueToFind the object to find
3668    * @return the last index of the value within the array,
3669    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3670    * @since 2.1
3671    */
 
3672  7 toggle public static int lastIndexOf(final char[] array, final char valueToFind) {
3673  7 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
3674    }
3675   
3676    /**
3677    * <p>Finds the last index of the given value in the array starting at the given index.
3678    *
3679    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3680    *
3681    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
3682    * array length will search from the end of the array.
3683    *
3684    * @param array the array to traverse for looking for the object, may be {@code null}
3685    * @param valueToFind the value to find
3686    * @param startIndex the start index to traverse backwards from
3687    * @return the last index of the value within the array,
3688    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3689    * @since 2.1
3690    */
 
3691  14 toggle public static int lastIndexOf(final char[] array, final char valueToFind, int startIndex) {
3692  14 if (array == null) {
3693  2 return INDEX_NOT_FOUND;
3694    }
3695  12 if (startIndex < 0) {
3696  1 return INDEX_NOT_FOUND;
3697  11 } else if (startIndex >= array.length) {
3698  7 startIndex = array.length - 1;
3699    }
3700  33 for (int i = startIndex; i >= 0; i--) {
3701  30 if (valueToFind == array[i]) {
3702  8 return i;
3703    }
3704    }
3705  3 return INDEX_NOT_FOUND;
3706    }
3707   
3708    /**
3709    * <p>Checks if the value is in the given array.
3710    *
3711    * <p>The method returns {@code false} if a {@code null} array is passed in.
3712    *
3713    * @param array the array to search through
3714    * @param valueToFind the value to find
3715    * @return {@code true} if the array contains the object
3716    * @since 2.1
3717    */
 
3718  16 toggle public static boolean contains(final char[] array, final char valueToFind) {
3719  16 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
3720    }
3721   
3722    // byte IndexOf
3723    //-----------------------------------------------------------------------
3724    /**
3725    * <p>Finds the index of the given value in the array.
3726    *
3727    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3728    *
3729    * @param array the array to search through for the object, may be {@code null}
3730    * @param valueToFind the value to find
3731    * @return the index of the value within the array,
3732    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3733    */
 
3734  33 toggle public static int indexOf(final byte[] array, final byte valueToFind) {
3735  33 return indexOf(array, valueToFind, 0);
3736    }
3737   
3738    /**
3739    * <p>Finds the index of the given value in the array starting at the given index.
3740    *
3741    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3742    *
3743    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3744    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3745    *
3746    * @param array the array to search through for the object, may be {@code null}
3747    * @param valueToFind the value to find
3748    * @param startIndex the index to start searching at
3749    * @return the index of the value within the array,
3750    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3751    */
 
3752  47 toggle public static int indexOf(final byte[] array, final byte valueToFind, int startIndex) {
3753  47 if (array == null) {
3754  5 return INDEX_NOT_FOUND;
3755    }
3756  42 if (startIndex < 0) {
3757  1 startIndex = 0;
3758    }
3759  130 for (int i = startIndex; i < array.length; i++) {
3760  119 if (valueToFind == array[i]) {
3761  31 return i;
3762    }
3763    }
3764  11 return INDEX_NOT_FOUND;
3765    }
3766   
3767    /**
3768    * <p>Finds the last index of the given value within the array.
3769    *
3770    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3771    *
3772    * @param array the array to traverse backwards looking for the object, may be {@code null}
3773    * @param valueToFind the object to find
3774    * @return the last index of the value within the array,
3775    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3776    */
 
3777  7 toggle public static int lastIndexOf(final byte[] array, final byte valueToFind) {
3778  7 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
3779    }
3780   
3781    /**
3782    * <p>Finds the last index of the given value in the array starting at the given index.
3783    *
3784    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3785    *
3786    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
3787    * array length will search from the end of the array.
3788    *
3789    * @param array the array to traverse for looking for the object, may be {@code null}
3790    * @param valueToFind the value to find
3791    * @param startIndex the start index to traverse backwards from
3792    * @return the last index of the value within the array,
3793    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3794    */
 
3795  14 toggle public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) {
3796  14 if (array == null) {
3797  2 return INDEX_NOT_FOUND;
3798    }
3799  12 if (startIndex < 0) {
3800  1 return INDEX_NOT_FOUND;
3801  11 } else if (startIndex >= array.length) {
3802  7 startIndex = array.length - 1;
3803    }
3804  33 for (int i = startIndex; i >= 0; i--) {
3805  30 if (valueToFind == array[i]) {
3806  8 return i;
3807    }
3808    }
3809  3 return INDEX_NOT_FOUND;
3810    }
3811   
3812    /**
3813    * <p>Checks if the value is in the given array.
3814    *
3815    * <p>The method returns {@code false} if a {@code null} array is passed in.
3816    *
3817    * @param array the array to search through
3818    * @param valueToFind the value to find
3819    * @return {@code true} if the array contains the object
3820    */
 
3821  16 toggle public static boolean contains(final byte[] array, final byte valueToFind) {
3822  16 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
3823    }
3824   
3825    // double IndexOf
3826    //-----------------------------------------------------------------------
3827    /**
3828    * <p>Finds the index of the given value in the array.
3829    *
3830    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3831    *
3832    * @param array the array to search through for the object, may be {@code null}
3833    * @param valueToFind the value to find
3834    * @return the index of the value within the array,
3835    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3836    */
 
3837  34 toggle public static int indexOf(final double[] array, final double valueToFind) {
3838  34 return indexOf(array, valueToFind, 0);
3839    }
3840   
3841    /**
3842    * <p>Finds the index of the given value within a given tolerance in the array.
3843    * This method will return the index of the first value which falls between the region
3844    * defined by valueToFind - tolerance and valueToFind + tolerance.
3845    *
3846    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3847    *
3848    * @param array the array to search through for the object, may be {@code null}
3849    * @param valueToFind the value to find
3850    * @param tolerance tolerance of the search
3851    * @return the index of the value within the array,
3852    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3853    */
 
3854  6 toggle public static int indexOf(final double[] array, final double valueToFind, final double tolerance) {
3855  6 return indexOf(array, valueToFind, 0, tolerance);
3856    }
3857   
3858    /**
3859    * <p>Finds the index of the given value in the array starting at the given index.
3860    *
3861    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3862    *
3863    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3864    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3865    *
3866    * @param array the array to search through for the object, may be {@code null}
3867    * @param valueToFind the value to find
3868    * @param startIndex the index to start searching at
3869    * @return the index of the value within the array,
3870    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3871    */
 
3872  49 toggle public static int indexOf(final double[] array, final double valueToFind, int startIndex) {
3873  49 if (ArrayUtils.isEmpty(array)) {
3874  9 return INDEX_NOT_FOUND;
3875    }
3876  40 if (startIndex < 0) {
3877  1 startIndex = 0;
3878    }
3879  128 for (int i = startIndex; i < array.length; i++) {
3880  119 if (valueToFind == array[i]) {
3881  31 return i;
3882    }
3883    }
3884  9 return INDEX_NOT_FOUND;
3885    }
3886   
3887    /**
3888    * <p>Finds the index of the given value in the array starting at the given index.
3889    * This method will return the index of the first value which falls between the region
3890    * defined by valueToFind - tolerance and valueToFind + tolerance.
3891    *
3892    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3893    *
3894    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
3895    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
3896    *
3897    * @param array the array to search through for the object, may be {@code null}
3898    * @param valueToFind the value to find
3899    * @param startIndex the index to start searching at
3900    * @param tolerance tolerance of the search
3901    * @return the index of the value within the array,
3902    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3903    */
 
3904  21 toggle public static int indexOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
3905  21 if (ArrayUtils.isEmpty(array)) {
3906  5 return INDEX_NOT_FOUND;
3907    }
3908  16 if (startIndex < 0) {
3909  2 startIndex = 0;
3910    }
3911  16 final double min = valueToFind - tolerance;
3912  16 final double max = valueToFind + tolerance;
3913  47 for (int i = startIndex; i < array.length; i++) {
3914  44 if (array[i] >= min && array[i] <= max) {
3915  13 return i;
3916    }
3917    }
3918  3 return INDEX_NOT_FOUND;
3919    }
3920   
3921    /**
3922    * <p>Finds the last index of the given value within the array.
3923    *
3924    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3925    *
3926    * @param array the array to traverse backwards looking for the object, may be {@code null}
3927    * @param valueToFind the object to find
3928    * @return the last index of the value within the array,
3929    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3930    */
 
3931  8 toggle public static int lastIndexOf(final double[] array, final double valueToFind) {
3932  8 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
3933    }
3934   
3935    /**
3936    * <p>Finds the last index of the given value within a given tolerance in the array.
3937    * This method will return the index of the last value which falls between the region
3938    * defined by valueToFind - tolerance and valueToFind + tolerance.
3939    *
3940    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3941    *
3942    * @param array the array to search through for the object, may be {@code null}
3943    * @param valueToFind the value to find
3944    * @param tolerance tolerance of the search
3945    * @return the index of the value within the array,
3946    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3947    */
 
3948  6 toggle public static int lastIndexOf(final double[] array, final double valueToFind, final double tolerance) {
3949  6 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance);
3950    }
3951   
3952    /**
3953    * <p>Finds the last index of the given value in the array starting at the given index.
3954    *
3955    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3956    *
3957    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
3958    * array length will search from the end of the array.
3959    *
3960    * @param array the array to traverse for looking for the object, may be {@code null}
3961    * @param valueToFind the value to find
3962    * @param startIndex the start index to traverse backwards from
3963    * @return the last index of the value within the array,
3964    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3965    */
 
3966  16 toggle public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex) {
3967  16 if (ArrayUtils.isEmpty(array)) {
3968  4 return INDEX_NOT_FOUND;
3969    }
3970  12 if (startIndex < 0) {
3971  1 return INDEX_NOT_FOUND;
3972  11 } else if (startIndex >= array.length) {
3973  7 startIndex = array.length - 1;
3974    }
3975  33 for (int i = startIndex; i >= 0; i--) {
3976  30 if (valueToFind == array[i]) {
3977  8 return i;
3978    }
3979    }
3980  3 return INDEX_NOT_FOUND;
3981    }
3982   
3983    /**
3984    * <p>Finds the last index of the given value in the array starting at the given index.
3985    * This method will return the index of the last value which falls between the region
3986    * defined by valueToFind - tolerance and valueToFind + tolerance.
3987    *
3988    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
3989    *
3990    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
3991    * array length will search from the end of the array.
3992    *
3993    * @param array the array to traverse for looking for the object, may be {@code null}
3994    * @param valueToFind the value to find
3995    * @param startIndex the start index to traverse backwards from
3996    * @param tolerance search for value within plus/minus this amount
3997    * @return the last index of the value within the array,
3998    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
3999    */
 
4000  15 toggle public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
4001  15 if (ArrayUtils.isEmpty(array)) {
4002  4 return INDEX_NOT_FOUND;
4003    }
4004  11 if (startIndex < 0) {
4005  1 return INDEX_NOT_FOUND;
4006  10 } else if (startIndex >= array.length) {
4007  7 startIndex = array.length - 1;
4008    }
4009  10 final double min = valueToFind - tolerance;
4010  10 final double max = valueToFind + tolerance;
4011  25 for (int i = startIndex; i >= 0; i--) {
4012  24 if (array[i] >= min && array[i] <= max) {
4013  9 return i;
4014    }
4015    }
4016  1 return INDEX_NOT_FOUND;
4017    }
4018   
4019    /**
4020    * <p>Checks if the value is in the given array.
4021    *
4022    * <p>The method returns {@code false} if a {@code null} array is passed in.
4023    *
4024    * @param array the array to search through
4025    * @param valueToFind the value to find
4026    * @return {@code true} if the array contains the object
4027    */
 
4028  16 toggle public static boolean contains(final double[] array, final double valueToFind) {
4029  16 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
4030    }
4031   
4032    /**
4033    * <p>Checks if a value falling within the given tolerance is in the
4034    * given array. If the array contains a value within the inclusive range
4035    * defined by (value - tolerance) to (value + tolerance).
4036    *
4037    * <p>The method returns {@code false} if a {@code null} array
4038    * is passed in.
4039    *
4040    * @param array the array to search
4041    * @param valueToFind the value to find
4042    * @param tolerance the array contains the tolerance of the search
4043    * @return true if value falling within tolerance is in array
4044    */
 
4045  5 toggle public static boolean contains(final double[] array, final double valueToFind, final double tolerance) {
4046  5 return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
4047    }
4048   
4049    // float IndexOf
4050    //-----------------------------------------------------------------------
4051    /**
4052    * <p>Finds the index of the given value in the array.
4053    *
4054    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
4055    *
4056    * @param array the array to search through for the object, may be {@code null}
4057    * @param valueToFind the value to find
4058    * @return the index of the value within the array,
4059    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
4060    */
 
4061  34 toggle public static int indexOf(final float[] array, final float valueToFind) {
4062  34 return indexOf(array, valueToFind, 0);
4063    }
4064   
4065    /**
4066    * <p>Finds the index of the given value in the array starting at the given index.
4067    *
4068    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
4069    *
4070    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
4071    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
4072    *
4073    * @param array the array to search through for the object, may be {@code null}
4074    * @param valueToFind the value to find
4075    * @param startIndex the index to start searching at
4076    * @return the index of the value within the array,
4077    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
4078    */
 
4079  49 toggle public static int indexOf(final float[] array, final float valueToFind, int startIndex) {
4080  49 if (ArrayUtils.isEmpty(array)) {
4081  9 return INDEX_NOT_FOUND;
4082    }
4083  40 if (startIndex < 0) {
4084  1 startIndex = 0;
4085    }
4086  128 for (int i = startIndex; i < array.length; i++) {
4087  119 if (valueToFind == array[i]) {
4088  31 return i;
4089    }
4090    }
4091  9 return INDEX_NOT_FOUND;
4092    }
4093   
4094    /**
4095    * <p>Finds the last index of the given value within the array.
4096    *
4097    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
4098    *
4099    * @param array the array to traverse backwards looking for the object, may be {@code null}
4100    * @param valueToFind the object to find
4101    * @return the last index of the value within the array,
4102    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
4103    */
 
4104  8 toggle public static int lastIndexOf(final float[] array, final float valueToFind) {
4105  8 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
4106    }
4107   
4108    /**
4109    * <p>Finds the last index of the given value in the array starting at the given index.
4110    *
4111    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
4112    *
4113    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
4114    * array length will search from the end of the array.
4115    *
4116    * @param array the array to traverse for looking for the object, may be {@code null}
4117    * @param valueToFind the value to find
4118    * @param startIndex the start index to traverse backwards from
4119    * @return the last index of the value within the array,
4120    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
4121    */
 
4122  16 toggle public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) {
4123  16 if (ArrayUtils.isEmpty(array)) {
4124  4 return INDEX_NOT_FOUND;
4125    }
4126  12 if (startIndex < 0) {
4127  1 return INDEX_NOT_FOUND;
4128  11 } else if (startIndex >= array.length) {
4129  7 startIndex = array.length - 1;
4130    }
4131  33 for (int i = startIndex; i >= 0; i--) {
4132  30 if (valueToFind == array[i]) {
4133  8 return i;
4134    }
4135    }
4136  3 return INDEX_NOT_FOUND;
4137    }
4138   
4139    /**
4140    * <p>Checks if the value is in the given array.
4141    *
4142    * <p>The method returns {@code false} if a {@code null} array is passed in.
4143    *
4144    * @param array the array to search through
4145    * @param valueToFind the value to find
4146    * @return {@code true} if the array contains the object
4147    */
 
4148  16 toggle public static boolean contains(final float[] array, final float valueToFind) {
4149  16 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
4150    }
4151   
4152    // boolean IndexOf
4153    //-----------------------------------------------------------------------
4154    /**
4155    * <p>Finds the index of the given value in the array.
4156    *
4157    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
4158    *
4159    * @param array the array to search through for the object, may be {@code null}
4160    * @param valueToFind the value to find
4161    * @return the index of the value within the array,
4162    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
4163    */
 
4164  22 toggle public static int indexOf(final boolean[] array, final boolean valueToFind) {
4165  22 return indexOf(array, valueToFind, 0);
4166    }
4167   
4168    /**
4169    * <p>Finds the index of the given value in the array starting at the given index.
4170    *
4171    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
4172    *
4173    * <p>A negative startIndex is treated as zero. A startIndex larger than the array
4174    * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
4175    *
4176    * @param array the array to search through for the object, may be {@code null}
4177    * @param valueToFind the value to find
4178    * @param startIndex the index to start searching at
4179    * @return the index of the value within the array,
4180    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null}
4181    * array input
4182    */
 
4183  43 toggle public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
4184  43 if (ArrayUtils.isEmpty(array)) {
4185  9 return INDEX_NOT_FOUND;
4186    }
4187  34 if (startIndex < 0) {
4188  2 startIndex = 0;
4189    }
4190  58 for (int i = startIndex; i < array.length; i++) {
4191  48 if (valueToFind == array[i]) {
4192  24 return i;
4193    }
4194    }
4195  10 return INDEX_NOT_FOUND;
4196    }
4197   
4198    /**
4199    * <p>Finds the last index of the given value within the array.
4200    *
4201    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) if
4202    * {@code null} array input.
4203    *
4204    * @param array the array to traverse backwards looking for the object, may be {@code null}
4205    * @param valueToFind the object to find
4206    * @return the last index of the value within the array,
4207    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
4208    */
 
4209  5 toggle public static int lastIndexOf(final boolean[] array, final boolean valueToFind) {
4210  5 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
4211    }
4212   
4213    /**
4214    * <p>Finds the last index of the given value in the array starting at the given index.
4215    *
4216    * <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
4217    *
4218    * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
4219    * the array length will search from the end of the array.
4220    *
4221    * @param array the array to traverse for looking for the object, may be {@code null}
4222    * @param valueToFind the value to find
4223    * @param startIndex the start index to traverse backwards from
4224    * @return the last index of the value within the array,
4225    * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
4226    */
 
4227  13 toggle public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
4228  13 if (ArrayUtils.isEmpty(array)) {
4229  4 return INDEX_NOT_FOUND;
4230    }
4231  9 if (startIndex < 0) {
4232  2 return INDEX_NOT_FOUND;
4233  7 } else if (startIndex >= array.length) {
4234  4 startIndex = array.length - 1;
4235    }
4236  14 for (int i = startIndex; i >= 0; i--) {
4237  12 if (valueToFind == array[i]) {
4238  5 return i;
4239    }
4240    }
4241  2 return INDEX_NOT_FOUND;
4242    }
4243   
4244    /**
4245    * <p>Checks if the value is in the given array.
4246    *
4247    * <p>The method returns {@code false} if a {@code null} array is passed in.
4248    *
4249    * @param array the array to search through
4250    * @param valueToFind the value to find
4251    * @return {@code true} if the array contains the object
4252    */
 
4253  5 toggle public static boolean contains(final boolean[] array, final boolean valueToFind) {
4254  5 return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
4255    }
4256   
4257    // Primitive/Object array converters
4258    // ----------------------------------------------------------------------
4259   
4260    // Character array converters
4261    // ----------------------------------------------------------------------
4262    /**
4263    * <p>Converts an array of object Characters to primitives.
4264    *
4265    * <p>This method returns {@code null} for a {@code null} input array.
4266    *
4267    * @param array a {@code Character} array, may be {@code null}
4268    * @return a {@code char} array, {@code null} if null array input
4269    * @throws NullPointerException if array content is {@code null}
4270    */
 
4271  4 toggle public static char[] toPrimitive(final Character[] array) {
4272  4 if (array == null) {
4273  1 return null;
4274  3 } else if (array.length == 0) {
4275  1 return EMPTY_CHAR_ARRAY;
4276    }
4277  2 final char[] result = new char[array.length];
4278  6 for (int i = 0; i < array.length; i++) {
4279  5 result[i] = array[i].charValue();
4280    }
4281  1 return result;
4282    }
4283   
4284    /**
4285    * <p>Converts an array of object Character to primitives handling {@code null}.
4286    *
4287    * <p>This method returns {@code null} for a {@code null} input array.
4288    *
4289    * @param array a {@code Character} array, may be {@code null}
4290    * @param valueForNull the value to insert if {@code null} found
4291    * @return a {@code char} array, {@code null} if null array input
4292    */
 
4293  4 toggle public static char[] toPrimitive(final Character[] array, final char valueForNull) {
4294  4 if (array == null) {
4295  1 return null;
4296  3 } else if (array.length == 0) {
4297  1 return EMPTY_CHAR_ARRAY;
4298    }
4299  2 final char[] result = new char[array.length];
4300  8 for (int i = 0; i < array.length; i++) {
4301  6 final Character b = array[i];
4302  6 result[i] = (b == null ? valueForNull : b.charValue());
4303    }
4304  2 return result;
4305    }
4306   
4307    /**
4308    * <p>Converts an array of primitive chars to objects.
4309    *
4310    * <p>This method returns {@code null} for a {@code null} input array.
4311    *
4312    * @param array a {@code char} array
4313    * @return a {@code Character} array, {@code null} if null array input
4314    */
 
4315  7 toggle public static Character[] toObject(final char[] array) {
4316  7 if (array == null) {
4317  1 return null;
4318  6 } else if (array.length == 0) {
4319  1 return EMPTY_CHARACTER_OBJECT_ARRAY;
4320    }
4321  5 final Character[] result = new Character[array.length];
4322  16 for (int i = 0; i < array.length; i++) {
4323  11 result[i] = Character.valueOf(array[i]);
4324    }
4325  5 return result;
4326    }
4327   
4328    // Long array converters
4329    // ----------------------------------------------------------------------
4330    /**
4331    * <p>Converts an array of object Longs to primitives.
4332    *
4333    * <p>This method returns {@code null} for a {@code null} input array.
4334    *
4335    * @param array a {@code Long} array, may be {@code null}
4336    * @return a {@code long} array, {@code null} if null array input
4337    * @throws NullPointerException if array content is {@code null}
4338    */
 
4339  7 toggle public static long[] toPrimitive(final Long[] array) {
4340  7 if (array == null) {
4341  1 return null;
4342  6 } else if (array.length == 0) {
4343  1 return EMPTY_LONG_ARRAY;
4344    }
4345  5 final long[] result = new long[array.length];
4346  15 for (int i = 0; i < array.length; i++) {
4347  11 result[i] = array[i].longValue();
4348    }
4349  4 return result;
4350    }
4351   
4352    /**
4353    * <p>Converts an array of object Long to primitives handling {@code null}.
4354    *
4355    * <p>This method returns {@code null} for a {@code null} input array.
4356    *
4357    * @param array a {@code Long} array, may be {@code null}
4358    * @param valueForNull the value to insert if {@code null} found
4359    * @return a {@code long} array, {@code null} if null array input
4360    */
 
4361  5 toggle public static long[] toPrimitive(final Long[] array, final long valueForNull) {
4362  5 if (array == null) {
4363  2 return null;
4364  3 } else if (array.length == 0) {
4365  1 return EMPTY_LONG_ARRAY;
4366    }
4367  2 final long[] result = new long[array.length];
4368  8 for (int i = 0; i < array.length; i++) {
4369  6 final Long b = array[i];
4370  6 result[i] = (b == null ? valueForNull : b.longValue());
4371    }
4372  2 return result;
4373    }
4374   
4375    /**
4376    * <p>Converts an array of primitive longs to objects.
4377    *
4378    * <p>This method returns {@code null} for a {@code null} input array.
4379    *
4380    * @param array a {@code long} array
4381    * @return a {@code Long} array, {@code null} if null array input
4382    */
 
4383  7 toggle public static Long[] toObject(final long[] array) {
4384  7 if (array == null) {
4385  1 return null;
4386  6 } else if (array.length == 0) {
4387  1 return EMPTY_LONG_OBJECT_ARRAY;
4388    }
4389  5 final Long[] result = new Long[array.length];
4390  16 for (int i = 0; i < array.length; i++) {
4391  11 result[i] = Long.valueOf(array[i]);
4392    }
4393  5 return result;
4394    }
4395   
4396    // Int array converters
4397    // ----------------------------------------------------------------------
4398    /**
4399    * <p>Converts an array of object Integers to primitives.
4400    *
4401    * <p>This method returns {@code null} for a {@code null} input array.
4402    *
4403    * @param array a {@code Integer} array, may be {@code null}
4404    * @return an {@code int} array, {@code null} if null array input
4405    * @throws NullPointerException if array content is {@code null}
4406    */
 
4407  7 toggle public static int[] toPrimitive(final Integer[] array) {
4408  7 if (array == null) {
4409  1 return null;
4410  6 } else if (array.length == 0) {
4411  2 return EMPTY_INT_ARRAY;
4412    }
4413  4 final int[] result = new int[array.length];
4414  12 for (int i = 0; i < array.length; i++) {
4415  9 result[i] = array[i].intValue();
4416    }
4417  3 return result;
4418    }
4419   
4420    /**
4421    * <p>Converts an array of object Integer to primitives handling {@code null}.
4422    *
4423    * <p>This method returns {@code null} for a {@code null} input array.
4424    *
4425    * @param array a {@code Integer} array, may be {@code null}
4426    * @param valueForNull the value to insert if {@code null} found
4427    * @return an {@code int} array, {@code null} if null array input
4428    */
 
4429  4 toggle public static int[] toPrimitive(final Integer[] array, final int valueForNull) {
4430  4 if (array == null) {
4431  1 return null;
4432  3 } else if (array.length == 0) {
4433  1 return EMPTY_INT_ARRAY;
4434    }
4435  2 final int[] result = new int[array.length];
4436  8 for (int i = 0; i < array.length; i++) {
4437  6 final Integer b = array[i];
4438  6 result[i] = (b == null ? valueForNull : b.intValue());
4439    }
4440  2 return result;
4441    }
4442   
4443    /**
4444    * <p>Converts an array of primitive ints to objects.
4445    *
4446    * <p>This method returns {@code null} for a {@code null} input array.
4447    *
4448    * @param array an {@code int} array
4449    * @return an {@code Integer} array, {@code null} if null array input
4450    */
 
4451  7 toggle public static Integer[] toObject(final int[] array) {
4452  7 if (array == null) {
4453  1 return null;
4454  6 } else if (array.length == 0) {
4455  1 return EMPTY_INTEGER_OBJECT_ARRAY;
4456    }
4457  5 final Integer[] result = new Integer[array.length];
4458  16 for (int i = 0; i < array.length; i++) {
4459  11 result[i] = Integer.valueOf(array[i]);
4460    }
4461  5 return result;
4462    }
4463   
4464    // Short array converters
4465    // ----------------------------------------------------------------------
4466    /**
4467    * <p>Converts an array of object Shorts to primitives.
4468    *
4469    * <p>This method returns {@code null} for a {@code null} input array.
4470    *
4471    * @param array a {@code Short} array, may be {@code null}
4472    * @return a {@code byte} array, {@code null} if null array input
4473    * @throws NullPointerException if array content is {@code null}
4474    */
 
4475  5 toggle public static short[] toPrimitive(final Short[] array) {
4476  5 if (array == null) {
4477  1 return null;
4478  4 } else if (array.length == 0) {
4479  1 return EMPTY_SHORT_ARRAY;
4480    }
4481  3 final short[] result = new short[array.length];
4482  8 for (int i = 0; i < array.length; i++) {
4483  6 result[i] = array[i].shortValue();
4484    }
4485  2 return result;
4486    }
4487   
4488    /**
4489    * <p>Converts an array of object Short to primitives handling {@code null}.
4490    *
4491    * <p>This method returns {@code null} for a {@code null} input array.
4492    *
4493    * @param array a {@code Short} array, may be {@code null}
4494    * @param valueForNull the value to insert if {@code null} found
4495    * @return a {@code byte} array, {@code null} if null array input
4496    */
 
4497  4 toggle public static short[] toPrimitive(final Short[] array, final short valueForNull) {
4498  4 if (array == null) {
4499  1 return null;
4500  3 } else if (array.length == 0) {
4501  1 return EMPTY_SHORT_ARRAY;
4502    }
4503  2 final short[] result = new short[array.length];
4504  8 for (int i = 0; i < array.length; i++) {
4505  6 final Short b = array[i];
4506  6 result[i] = (b == null ? valueForNull : b.shortValue());
4507    }
4508  2 return result;
4509    }
4510   
4511    /**
4512    * <p>Converts an array of primitive shorts to objects.
4513    *
4514    * <p>This method returns {@code null} for a {@code null} input array.
4515    *
4516    * @param array a {@code short} array
4517    * @return a {@code Short} array, {@code null} if null array input
4518    */
 
4519  7 toggle public static Short[] toObject(final short[] array) {
4520  7 if (array == null) {
4521  1 return null;
4522  6 } else if (array.length == 0) {
4523  1 return EMPTY_SHORT_OBJECT_ARRAY;
4524    }
4525  5 final Short[] result = new Short[array.length];
4526  16 for (int i = 0; i < array.length; i++) {
4527  11 result[i] = Short.valueOf(array[i]);
4528    }
4529  5 return result;
4530    }
4531   
4532    // Byte array converters
4533    // ----------------------------------------------------------------------
4534    /**
4535    * <p>Converts an array of object Bytes to primitives.
4536    *
4537    * <p>This method returns {@code null} for a {@code null} input array.
4538    *
4539    * @param array a {@code Byte} array, may be {@code null}
4540    * @return a {@code byte} array, {@code null} if null array input
4541    * @throws NullPointerException if array content is {@code null}
4542    */
 
4543  4 toggle public static byte[] toPrimitive(final Byte[] array) {
4544  4 if (array == null) {
4545  1 return null;
4546  3 } else if (array.length == 0) {
4547  1 return EMPTY_BYTE_ARRAY;
4548    }
4549  2 final byte[] result = new byte[array.length];
4550  6 for (int i = 0; i < array.length; i++) {
4551  5 result[i] = array[i].byteValue();
4552    }
4553  1 return result;
4554    }
4555   
4556    /**
4557    * <p>Converts an array of object Bytes to primitives handling {@code null}.
4558    *
4559    * <p>This method returns {@code null} for a {@code null} input array.
4560    *
4561    * @param array a {@code Byte} array, may be {@code null}
4562    * @param valueForNull the value to insert if {@code null} found
4563    * @return a {@code byte} array, {@code null} if null array input
4564    */
 
4565  4 toggle public static byte[] toPrimitive(final Byte[] array, final byte valueForNull) {
4566  4 if (array == null) {
4567  1 return null;
4568  3 } else if (array.length == 0) {
4569  1 return EMPTY_BYTE_ARRAY;
4570    }
4571  2 final byte[] result = new byte[array.length];
4572  8 for (int i = 0; i < array.length; i++) {
4573  6 final Byte b = array[i];
4574  6 result[i] = (b == null ? valueForNull : b.byteValue());
4575    }
4576  2 return result;
4577    }
4578   
4579    /**
4580    * <p>Converts an array of primitive bytes to objects.
4581    *
4582    * <p>This method returns {@code null} for a {@code null} input array.
4583    *
4584    * @param array a {@code byte} array
4585    * @return a {@code Byte} array, {@code null} if null array input
4586    */
 
4587  7 toggle public static Byte[] toObject(final byte[] array) {
4588  7 if (array == null) {
4589  1 return null;
4590  6 } else if (array.length == 0) {
4591  1 return EMPTY_BYTE_OBJECT_ARRAY;
4592    }
4593  5 final Byte[] result = new Byte[array.length];
4594  14 for (int i = 0; i < array.length; i++) {
4595  9 result[i] = Byte.valueOf(array[i]);
4596    }
4597  5 return result;
4598    }
4599   
4600    // Double array converters
4601    // ----------------------------------------------------------------------
4602    /**
4603    * <p>Converts an array of object Doubles to primitives.
4604    *
4605    * <p>This method returns {@code null} for a {@code null} input array.
4606    *
4607    * @param array a {@code Double} array, may be {@code null}
4608    * @return a {@code double} array, {@code null} if null array input
4609    * @throws NullPointerException if array content is {@code null}
4610    */
 
4611  4 toggle public static double[] toPrimitive(final Double[] array) {
4612  4 if (array == null) {
4613  1 return null;
4614  3 } else if (array.length == 0) {
4615  1 return EMPTY_DOUBLE_ARRAY;
4616    }
4617  2 final double[] result = new double[array.length];
4618  6 for (int i = 0; i < array.length; i++) {
4619  4 result[i] = array[i].doubleValue();
4620    }
4621  2 return result;
4622    }
4623   
4624    /**
4625    * <p>Converts an array of object Doubles to primitives handling {@code null}.
4626    *
4627    * <p>This method returns {@code null} for a {@code null} input array.
4628    *
4629    * @param array a {@code Double} array, may be {@code null}
4630    * @param valueForNull the value to insert if {@code null} found
4631    * @return a {@code double} array, {@code null} if null array input
4632    */
 
4633  4 toggle public static double[] toPrimitive(final Double[] array, final double valueForNull) {
4634  4 if (array == null) {
4635  1 return null;
4636  3 } else if (array.length == 0) {
4637  1 return EMPTY_DOUBLE_ARRAY;
4638    }
4639  2 final double[] result = new double[array.length];
4640  8 for (int i = 0; i < array.length; i++) {
4641  6 final Double b = array[i];
4642  6 result[i] = (b == null ? valueForNull : b.doubleValue());
4643    }
4644  2 return result;
4645    }
4646   
4647    /**
4648    * <p>Converts an array of primitive doubles to objects.
4649    *
4650    * <p>This method returns {@code null} for a {@code null} input array.
4651    *
4652    * @param array a {@code double} array
4653    * @return a {@code Double} array, {@code null} if null array input
4654    */
 
4655  7 toggle public static Double[] toObject(final double[] array) {
4656  7 if (array == null) {
4657  1 return null;
4658  6 } else if (array.length == 0) {
4659  1 return EMPTY_DOUBLE_OBJECT_ARRAY;
4660    }
4661  5 final Double[] result = new Double[array.length];
4662  16 for (int i = 0; i < array.length; i++) {
4663  11 result[i] = Double.valueOf(array[i]);
4664    }
4665  5 return result;
4666    }
4667   
4668    // Float array converters
4669    // ----------------------------------------------------------------------
4670    /**
4671    * <p>Converts an array of object Floats to primitives.
4672    *
4673    * <p>This method returns {@code null} for a {@code null} input array.
4674    *
4675    * @param array a {@code Float} array, may be {@code null}
4676    * @return a {@code float} array, {@code null} if null array input
4677    * @throws NullPointerException if array content is {@code null}
4678    */
 
4679  6 toggle public static float[] toPrimitive(final Float[] array) {
4680  6 if (array == null) {
4681  1 return null;
4682  5 } else if (array.length == 0) {
4683  1 return EMPTY_FLOAT_ARRAY;
4684    }
4685  4 final float[] result = new float[array.length];
4686  10 for (int i = 0; i < array.length; i++) {
4687  8 result[i] = array[i].floatValue();
4688    }
4689  2 return result;
4690    }
4691   
4692    /**
4693    * <p>Converts an array of object Floats to primitives handling {@code null}.
4694    *
4695    * <p>This method returns {@code null} for a {@code null} input array.
4696    *
4697    * @param array a {@code Float} array, may be {@code null}
4698    * @param valueForNull the value to insert if {@code null} found
4699    * @return a {@code float} array, {@code null} if null array input
4700    */
 
4701  4 toggle public static float[] toPrimitive(final Float[] array, final float valueForNull) {
4702  4 if (array == null) {
4703  1 return null;
4704  3 } else if (array.length == 0) {
4705  1 return EMPTY_FLOAT_ARRAY;
4706    }
4707  2 final float[] result = new float[array.length];
4708  8 for (int i = 0; i < array.length; i++) {
4709  6 final Float b = array[i];
4710  6 result[i] = (b == null ? valueForNull : b.floatValue());
4711    }
4712  2 return result;
4713    }
4714   
4715    /**
4716    * <p>Converts an array of primitive floats to objects.
4717    *
4718    * <p>This method returns {@code null} for a {@code null} input array.
4719    *
4720    * @param array a {@code float} array
4721    * @return a {@code Float} array, {@code null} if null array input
4722    */
 
4723  7 toggle public static Float[] toObject(final float[] array) {
4724  7 if (array == null) {
4725  1 return null;
4726  6 } else if (array.length == 0) {
4727  1 return EMPTY_FLOAT_OBJECT_ARRAY;
4728    }
4729  5 final Float[] result = new Float[array.length];
4730  16 for (int i = 0; i < array.length; i++) {
4731  11 result[i] = Float.valueOf(array[i]);
4732    }
4733  5 return result;
4734    }
4735   
4736    /**
4737    * <p>Create an array of primitive type from an array of wrapper types.
4738    *
4739    * <p>This method returns {@code null} for a {@code null} input array.
4740    *
4741    * @param array an array of wrapper object
4742    * @return an array of the corresponding primitive type, or the original array
4743    * @since 3.5
4744    */
 
4745  5 toggle public static Object toPrimitive(final Object array) {
4746  5 if (array == null) {
4747  1 return null;
4748    }
4749  4 final Class<?> ct = array.getClass().getComponentType();
4750  4 final Class<?> pt = ClassUtils.wrapperToPrimitive(ct);
4751  4 if(Integer.TYPE.equals(pt)) {
4752  2 return toPrimitive((Integer[]) array);
4753    }
4754  2 if(Long.TYPE.equals(pt)) {
4755  2 return toPrimitive((Long[]) array);
4756    }
4757  0 if(Short.TYPE.equals(pt)) {
4758  0 return toPrimitive((Short[]) array);
4759    }
4760  0 if(Double.TYPE.equals(pt)) {
4761  0 return toPrimitive((Double[]) array);
4762    }
4763  0 if(Float.TYPE.equals(pt)) {
4764  0 return toPrimitive((Float[]) array);
4765    }
4766  0 return array;
4767    }
4768   
4769    // Boolean array converters
4770    // ----------------------------------------------------------------------
4771    /**
4772    * <p>Converts an array of object Booleans to primitives.
4773    *
4774    * <p>This method returns {@code null} for a {@code null} input array.
4775    *
4776    * @param array a {@code Boolean} array, may be {@code null}
4777    * @return a {@code boolean} array, {@code null} if null array input
4778    * @throws NullPointerException if array content is {@code null}
4779    */
 
4780  43 toggle public static boolean[] toPrimitive(final Boolean[] array) {
4781  43 if (array == null) {
4782  1 return null;
4783  42 } else if (array.length == 0) {
4784  1 return EMPTY_BOOLEAN_ARRAY;
4785    }
4786  41 final boolean[] result = new boolean[array.length];
4787  141 for (int i = 0; i < array.length; i++) {
4788  104 result[i] = array[i].booleanValue();
4789    }
4790  37 return result;
4791    }
4792   
4793    /**
4794    * <p>Converts an array of object Booleans to primitives handling {@code null}.
4795    *
4796    * <p>This method returns {@code null} for a {@code null} input array.
4797    *
4798    * @param array a {@code Boolean} array, may be {@code null}
4799    * @param valueForNull the value to insert if {@code null} found
4800    * @return a {@code boolean} array, {@code null} if null array input
4801    */
 
4802  5 toggle public static boolean[] toPrimitive(final Boolean[] array, final boolean valueForNull) {
4803  5 if (array == null) {
4804  1 return null;
4805  4 } else if (array.length == 0) {
4806  1 return EMPTY_BOOLEAN_ARRAY;
4807    }
4808  3 final boolean[] result = new boolean[array.length];
4809  12 for (int i = 0; i < array.length; i++) {
4810  9 final Boolean b = array[i];
4811  9 result[i] = (b == null ? valueForNull : b.booleanValue());
4812    }
4813  3 return result;
4814    }
4815   
4816    /**
4817    * <p>Converts an array of primitive booleans to objects.
4818    *
4819    * <p>This method returns {@code null} for a {@code null} input array.
4820    *
4821    * @param array a {@code boolean} array
4822    * @return a {@code Boolean} array, {@code null} if null array input
4823    */
 
4824  7 toggle public static Boolean[] toObject(final boolean[] array) {
4825  7 if (array == null) {
4826  1 return null;
4827  6 } else if (array.length == 0) {
4828  1 return EMPTY_BOOLEAN_OBJECT_ARRAY;
4829    }
4830  5 final Boolean[] result = new Boolean[array.length];
4831  14 for (int i = 0; i < array.length; i++) {
4832  9 result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
4833    }
4834  5 return result;
4835    }
4836   
4837    // ----------------------------------------------------------------------
4838    /**
4839    * <p>Checks if an array of Objects is empty or {@code null}.
4840    *
4841    * @param array the array to test
4842    * @return {@code true} if the array is empty or {@code null}
4843    * @since 2.1
4844    */
 
4845  742 toggle public static boolean isEmpty(final Object[] array) {
4846  742 return getLength(array) == 0;
4847    }
4848   
4849    /**
4850    * <p>Checks if an array of primitive longs is empty or {@code null}.
4851    *
4852    * @param array the array to test
4853    * @return {@code true} if the array is empty or {@code null}
4854    * @since 2.1
4855    */
 
4856  31 toggle public static boolean isEmpty(final long[] array) {
4857  31 return getLength(array) == 0;
4858    }
4859   
4860    /**
4861    * <p>Checks if an array of primitive ints is empty or {@code null}.
4862    *
4863    * @param array the array to test
4864    * @return {@code true} if the array is empty or {@code null}
4865    * @since 2.1
4866    */
 
4867  90212 toggle public static boolean isEmpty(final int[] array) {
4868  90212 return getLength(array) == 0;
4869    }
4870   
4871    /**
4872    * <p>Checks if an array of primitive shorts is empty or {@code null}.
4873    *
4874    * @param array the array to test
4875    * @return {@code true} if the array is empty or {@code null}
4876    * @since 2.1
4877    */
 
4878  31 toggle public static boolean isEmpty(final short[] array) {
4879  31 return getLength(array) == 0;
4880    }
4881   
4882    /**
4883    * <p>Checks if an array of primitive chars is empty or {@code null}.
4884    *
4885    * @param array the array to test
4886    * @return {@code true} if the array is empty or {@code null}
4887    * @since 2.1
4888    */
 
4889  121 toggle public static boolean isEmpty(final char[] array) {
4890  121 return getLength(array) == 0;
4891    }
4892   
4893    /**
4894    * <p>Checks if an array of primitive bytes is empty or {@code null}.
4895    *
4896    * @param array the array to test
4897    * @return {@code true} if the array is empty or {@code null}
4898    * @since 2.1
4899    */
 
4900  31 toggle public static boolean isEmpty(final byte[] array) {
4901  31 return getLength(array) == 0;
4902    }
4903   
4904    /**
4905    * <p>Checks if an array of primitive doubles is empty or {@code null}.
4906    *
4907    * @param array the array to test
4908    * @return {@code true} if the array is empty or {@code null}
4909    * @since 2.1
4910    */
 
4911  132 toggle public static boolean isEmpty(final double[] array) {
4912  132 return getLength(array) == 0;
4913    }
4914   
4915    /**
4916    * <p>Checks if an array of primitive floats is empty or {@code null}.
4917    *
4918    * @param array the array to test
4919    * @return {@code true} if the array is empty or {@code null}
4920    * @since 2.1
4921    */
 
4922  96 toggle public static boolean isEmpty(final float[] array) {
4923  96 return getLength(array) == 0;
4924    }
4925   
4926    /**
4927    * <p>Checks if an array of primitive booleans is empty or {@code null}.
4928    *
4929    * @param array the array to test
4930    * @return {@code true} if the array is empty or {@code null}
4931    * @since 2.1
4932    */
 
4933  87 toggle public static boolean isEmpty(final boolean[] array) {
4934  87 return getLength(array) == 0;
4935    }
4936   
4937    // ----------------------------------------------------------------------
4938    /**
4939    * <p>Checks if an array of Objects is not empty and not {@code null}.
4940    *
4941    * @param <T> the component type of the array
4942    * @param array the array to test
4943    * @return {@code true} if the array is not empty and not {@code null}
4944    * @since 2.5
4945    */
 
4946  48 toggle public static <T> boolean isNotEmpty(final T[] array) {
4947  48 return !isEmpty(array);
4948    }
4949   
4950    /**
4951    * <p>Checks if an array of primitive longs is not empty and not {@code null}.
4952    *
4953    * @param array the array to test
4954    * @return {@code true} if the array is not empty and not {@code null}
4955    * @since 2.5
4956    */
 
4957  3 toggle public static boolean isNotEmpty(final long[] array) {
4958  3 return !isEmpty(array);
4959    }
4960   
4961    /**
4962    * <p>Checks if an array of primitive ints is not empty and not {@code null}.
4963    *
4964    * @param array the array to test
4965    * @return {@code true} if the array is not empty and not {@code null}
4966    * @since 2.5
4967    */
 
4968  90184 toggle public static boolean isNotEmpty(final int[] array) {
4969  90184 return !isEmpty(array);
4970    }
4971   
4972    /**
4973    * <p>Checks if an array of primitive shorts is not empty and not {@code null}.
4974    *
4975    * @param array the array to test
4976    * @return {@code true} if the array is not empty and not {@code null}
4977    * @since 2.5
4978    */
 
4979  3 toggle public static boolean isNotEmpty(final short[] array) {
4980  3 return !isEmpty(array);
4981    }
4982   
4983    /**
4984    * <p>Checks if an array of primitive chars is not empty and not {@code null}.
4985    *
4986    * @param array the array to test
4987    * @return {@code true} if the array is not empty and not {@code null}
4988    * @since 2.5
4989    */
 
4990  3 toggle public static boolean isNotEmpty(final char[] array) {
4991  3 return !isEmpty(array);
4992    }
4993   
4994    /**
4995    * <p>Checks if an array of primitive bytes is not empty and not {@code null}.
4996    *
4997    * @param array the array to test
4998    * @return {@code true} if the array is not empty and not {@code null}
4999    * @since 2.5
5000    */
 
5001  3 toggle public static boolean isNotEmpty(final byte[] array) {
5002  3 return !isEmpty(array);
5003    }
5004   
5005    /**
5006    * <p>Checks if an array of primitive doubles is not empty and not {@code null}.
5007    *
5008    * @param array the array to test
5009    * @return {@code true} if the array is not empty and not {@code null}
5010    * @since 2.5
5011    */
 
5012  3 toggle public static boolean isNotEmpty(final double[] array) {
5013  3 return !isEmpty(array);
5014    }
5015   
5016    /**
5017    * <p>Checks if an array of primitive floats is not empty and not {@code null}.
5018    *
5019    * @param array the array to test
5020    * @return {@code true} if the array is not empty and not {@code null}
5021    * @since 2.5
5022    */
 
5023  3 toggle public static boolean isNotEmpty(final float[] array) {
5024  3 return !isEmpty(array);
5025    }
5026   
5027    /**
5028    * <p>Checks if an array of primitive booleans is not empty and not {@code null}.
5029    *
5030    * @param array the array to test
5031    * @return {@code true} if the array is not empty and not {@code null}
5032    * @since 2.5
5033    */
 
5034  3 toggle public static boolean isNotEmpty(final boolean[] array) {
5035  3 return !isEmpty(array);
5036    }
5037   
5038    /**
5039    * <p>Adds all the elements of the given arrays into a new array.
5040    * <p>The new array contains all of the element of {@code array1} followed
5041    * by all of the elements {@code array2}. When an array is returned, it is always
5042    * a new array.
5043    *
5044    * <pre>
5045    * ArrayUtils.addAll(null, null) = null
5046    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5047    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5048    * ArrayUtils.addAll([], []) = []
5049    * ArrayUtils.addAll([null], [null]) = [null, null]
5050    * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
5051    * </pre>
5052    *
5053    * @param <T> the component type of the array
5054    * @param array1 the first array whose elements are added to the new array, may be {@code null}
5055    * @param array2 the second array whose elements are added to the new array, may be {@code null}
5056    * @return The new array, {@code null} if both arrays are {@code null}.
5057    * The type of the new array is the type of the first array,
5058    * unless the first array is null, in which case the type is the same as the second array.
5059    * @since 2.1
5060    * @throws IllegalArgumentException if the array types are incompatible
5061    */
 
5062  24 toggle public static <T> T[] addAll(final T[] array1, final T... array2) {
5063  24 if (array1 == null) {
5064  3 return clone(array2);
5065  21 } else if (array2 == null) {
5066  2 return clone(array1);
5067    }
5068  19 final Class<?> type1 = array1.getClass().getComponentType();
5069  19 @SuppressWarnings("unchecked") // OK, because array is of type T
5070    final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
5071  19 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5072  19 try {
5073  19 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5074    } catch (final ArrayStoreException ase) {
5075    // Check if problem was due to incompatible types
5076    /*
5077    * We do this here, rather than before the copy because:
5078    * - it would be a wasted check most of the time
5079    * - safer, in case check turns out to be too strict
5080    */
5081  1 final Class<?> type2 = array2.getClass().getComponentType();
5082  1 if (!type1.isAssignableFrom(type2)) {
5083  1 throw new IllegalArgumentException("Cannot store " + type2.getName() + " in an array of "
5084    + type1.getName(), ase);
5085    }
5086  0 throw ase; // No, so rethrow original
5087    }
5088  18 return joinedArray;
5089    }
5090   
5091    /**
5092    * <p>Adds all the elements of the given arrays into a new array.
5093    * <p>The new array contains all of the element of {@code array1} followed
5094    * by all of the elements {@code array2}. When an array is returned, it is always
5095    * a new array.
5096    *
5097    * <pre>
5098    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5099    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5100    * ArrayUtils.addAll([], []) = []
5101    * </pre>
5102    *
5103    * @param array1 the first array whose elements are added to the new array.
5104    * @param array2 the second array whose elements are added to the new array.
5105    * @return The new boolean[] array.
5106    * @since 2.1
5107    */
 
5108  3 toggle public static boolean[] addAll(final boolean[] array1, final boolean... array2) {
5109  3 if (array1 == null) {
5110  1 return clone(array2);
5111  2 } else if (array2 == null) {
5112  1 return clone(array1);
5113    }
5114  1 final boolean[] joinedArray = new boolean[array1.length + array2.length];
5115  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5116  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5117  1 return joinedArray;
5118    }
5119   
5120    /**
5121    * <p>Adds all the elements of the given arrays into a new array.
5122    * <p>The new array contains all of the element of {@code array1} followed
5123    * by all of the elements {@code array2}. When an array is returned, it is always
5124    * a new array.
5125    *
5126    * <pre>
5127    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5128    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5129    * ArrayUtils.addAll([], []) = []
5130    * </pre>
5131    *
5132    * @param array1 the first array whose elements are added to the new array.
5133    * @param array2 the second array whose elements are added to the new array.
5134    * @return The new char[] array.
5135    * @since 2.1
5136    */
 
5137  3 toggle public static char[] addAll(final char[] array1, final char... array2) {
5138  3 if (array1 == null) {
5139  1 return clone(array2);
5140  2 } else if (array2 == null) {
5141  1 return clone(array1);
5142    }
5143  1 final char[] joinedArray = new char[array1.length + array2.length];
5144  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5145  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5146  1 return joinedArray;
5147    }
5148   
5149    /**
5150    * <p>Adds all the elements of the given arrays into a new array.
5151    * <p>The new array contains all of the element of {@code array1} followed
5152    * by all of the elements {@code array2}. When an array is returned, it is always
5153    * a new array.
5154    *
5155    * <pre>
5156    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5157    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5158    * ArrayUtils.addAll([], []) = []
5159    * </pre>
5160    *
5161    * @param array1 the first array whose elements are added to the new array.
5162    * @param array2 the second array whose elements are added to the new array.
5163    * @return The new byte[] array.
5164    * @since 2.1
5165    */
 
5166  3 toggle public static byte[] addAll(final byte[] array1, final byte... array2) {
5167  3 if (array1 == null) {
5168  1 return clone(array2);
5169  2 } else if (array2 == null) {
5170  1 return clone(array1);
5171    }
5172  1 final byte[] joinedArray = new byte[array1.length + array2.length];
5173  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5174  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5175  1 return joinedArray;
5176    }
5177   
5178    /**
5179    * <p>Adds all the elements of the given arrays into a new array.
5180    * <p>The new array contains all of the element of {@code array1} followed
5181    * by all of the elements {@code array2}. When an array is returned, it is always
5182    * a new array.
5183    *
5184    * <pre>
5185    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5186    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5187    * ArrayUtils.addAll([], []) = []
5188    * </pre>
5189    *
5190    * @param array1 the first array whose elements are added to the new array.
5191    * @param array2 the second array whose elements are added to the new array.
5192    * @return The new short[] array.
5193    * @since 2.1
5194    */
 
5195  3 toggle public static short[] addAll(final short[] array1, final short... array2) {
5196  3 if (array1 == null) {
5197  1 return clone(array2);
5198  2 } else if (array2 == null) {
5199  1 return clone(array1);
5200    }
5201  1 final short[] joinedArray = new short[array1.length + array2.length];
5202  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5203  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5204  1 return joinedArray;
5205    }
5206   
5207    /**
5208    * <p>Adds all the elements of the given arrays into a new array.
5209    * <p>The new array contains all of the element of {@code array1} followed
5210    * by all of the elements {@code array2}. When an array is returned, it is always
5211    * a new array.
5212    *
5213    * <pre>
5214    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5215    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5216    * ArrayUtils.addAll([], []) = []
5217    * </pre>
5218    *
5219    * @param array1 the first array whose elements are added to the new array.
5220    * @param array2 the second array whose elements are added to the new array.
5221    * @return The new int[] array.
5222    * @since 2.1
5223    */
 
5224  3 toggle public static int[] addAll(final int[] array1, final int... array2) {
5225  3 if (array1 == null) {
5226  1 return clone(array2);
5227  2 } else if (array2 == null) {
5228  1 return clone(array1);
5229    }
5230  1 final int[] joinedArray = new int[array1.length + array2.length];
5231  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5232  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5233  1 return joinedArray;
5234    }
5235   
5236    /**
5237    * <p>Adds all the elements of the given arrays into a new array.
5238    * <p>The new array contains all of the element of {@code array1} followed
5239    * by all of the elements {@code array2}. When an array is returned, it is always
5240    * a new array.
5241    *
5242    * <pre>
5243    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5244    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5245    * ArrayUtils.addAll([], []) = []
5246    * </pre>
5247    *
5248    * @param array1 the first array whose elements are added to the new array.
5249    * @param array2 the second array whose elements are added to the new array.
5250    * @return The new long[] array.
5251    * @since 2.1
5252    */
 
5253  3 toggle public static long[] addAll(final long[] array1, final long... array2) {
5254  3 if (array1 == null) {
5255  1 return clone(array2);
5256  2 } else if (array2 == null) {
5257  1 return clone(array1);
5258    }
5259  1 final long[] joinedArray = new long[array1.length + array2.length];
5260  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5261  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5262  1 return joinedArray;
5263    }
5264   
5265    /**
5266    * <p>Adds all the elements of the given arrays into a new array.
5267    * <p>The new array contains all of the element of {@code array1} followed
5268    * by all of the elements {@code array2}. When an array is returned, it is always
5269    * a new array.
5270    *
5271    * <pre>
5272    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5273    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5274    * ArrayUtils.addAll([], []) = []
5275    * </pre>
5276    *
5277    * @param array1 the first array whose elements are added to the new array.
5278    * @param array2 the second array whose elements are added to the new array.
5279    * @return The new float[] array.
5280    * @since 2.1
5281    */
 
5282  3 toggle public static float[] addAll(final float[] array1, final float... array2) {
5283  3 if (array1 == null) {
5284  1 return clone(array2);
5285  2 } else if (array2 == null) {
5286  1 return clone(array1);
5287    }
5288  1 final float[] joinedArray = new float[array1.length + array2.length];
5289  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5290  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5291  1 return joinedArray;
5292    }
5293   
5294    /**
5295    * <p>Adds all the elements of the given arrays into a new array.
5296    * <p>The new array contains all of the element of {@code array1} followed
5297    * by all of the elements {@code array2}. When an array is returned, it is always
5298    * a new array.
5299    *
5300    * <pre>
5301    * ArrayUtils.addAll(array1, null) = cloned copy of array1
5302    * ArrayUtils.addAll(null, array2) = cloned copy of array2
5303    * ArrayUtils.addAll([], []) = []
5304    * </pre>
5305    *
5306    * @param array1 the first array whose elements are added to the new array.
5307    * @param array2 the second array whose elements are added to the new array.
5308    * @return The new double[] array.
5309    * @since 2.1
5310    */
 
5311  3 toggle public static double[] addAll(final double[] array1, final double... array2) {
5312  3 if (array1 == null) {
5313  1 return clone(array2);
5314  2 } else if (array2 == null) {
5315  1 return clone(array1);
5316    }
5317  1 final double[] joinedArray = new double[array1.length + array2.length];
5318  1 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
5319  1 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
5320  1 return joinedArray;
5321    }
5322   
5323    /**
5324    * <p>Copies the given array and adds the given element at the end of the new array.
5325    *
5326    * <p>The new array contains the same elements of the input
5327    * array plus the given element in the last position. The component type of
5328    * the new array is the same as that of the input array.
5329    *
5330    * <p>If the input array is {@code null}, a new one element array is returned
5331    * whose component type is the same as the element, unless the element itself is null,
5332    * in which case the return type is Object[]
5333    *
5334    * <pre>
5335    * ArrayUtils.add(null, null) = IllegalArgumentException
5336    * ArrayUtils.add(null, "a") = ["a"]
5337    * ArrayUtils.add(["a"], null) = ["a", null]
5338    * ArrayUtils.add(["a"], "b") = ["a", "b"]
5339    * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
5340    * </pre>
5341    *
5342    * @param <T> the component type of the array
5343    * @param array the array to "add" the element to, may be {@code null}
5344    * @param element the object to add, may be {@code null}
5345    * @return A new array containing the existing elements plus the new element
5346    * The returned array type will be that of the input array (unless null),
5347    * in which case it will have the same type as the element.
5348    * If both are null, an IllegalArgumentException is thrown
5349    * @since 2.1
5350    * @throws IllegalArgumentException if both arguments are null
5351    */
 
5352  11 toggle public static <T> T[] add(final T[] array, final T element) {
5353  11 Class<?> type;
5354  11 if (array != null) {
5355  7 type = array.getClass().getComponentType();
5356  4 } else if (element != null) {
5357  3 type = element.getClass();
5358    } else {
5359  1 throw new IllegalArgumentException("Arguments cannot both be null");
5360    }
5361  10 @SuppressWarnings("unchecked") // type must be T
5362    final
5363    T[] newArray = (T[]) copyArrayGrow1(array, type);
5364  10 newArray[newArray.length - 1] = element;
5365  10 return newArray;
5366    }
5367   
5368    /**
5369    * <p>Copies the given array and adds the given element at the end of the new array.
5370    *
5371    * <p>The new array contains the same elements of the input
5372    * array plus the given element in the last position. The component type of
5373    * the new array is the same as that of the input array.
5374    *
5375    * <p>If the input array is {@code null}, a new one element array is returned
5376    * whose component type is the same as the element.
5377    *
5378    * <pre>
5379    * ArrayUtils.add(null, true) = [true]
5380    * ArrayUtils.add([true], false) = [true, false]
5381    * ArrayUtils.add([true, false], true) = [true, false, true]
5382    * </pre>
5383    *
5384    * @param array the array to copy and add the element to, may be {@code null}
5385    * @param element the object to add at the last index of the new array
5386    * @return A new array containing the existing elements plus the new element
5387    * @since 2.1
5388    */
 
5389  3 toggle public static boolean[] add(final boolean[] array, final boolean element) {
5390  3 final boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE);
5391  3 newArray[newArray.length - 1] = element;
5392  3 return newArray;
5393    }
5394   
5395    /**
5396    * <p>Copies the given array and adds the given element at the end of the new array.
5397    *
5398    * <p>The new array contains the same elements of the input
5399    * array plus the given element in the last position. The component type of
5400    * the new array is the same as that of the input array.
5401    *
5402    * <p>If the input array is {@code null}, a new one element array is returned
5403    * whose component type is the same as the element.
5404    *
5405    * <pre>
5406    * ArrayUtils.add(null, 0) = [0]
5407    * ArrayUtils.add([1], 0) = [1, 0]
5408    * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
5409    * </pre>
5410    *
5411    * @param array the array to copy and add the element to, may be {@code null}
5412    * @param element the object to add at the last index of the new array
5413    * @return A new array containing the existing elements plus the new element
5414    * @since 2.1
5415    */
 
5416  4 toggle public static byte[] add(final byte[] array, final byte element) {
5417  4 final byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE);
5418  4 newArray[newArray.length - 1] = element;
5419  4 return newArray;
5420    }
5421   
5422    /**
5423    * <p>Copies the given array and adds the given element at the end of the new array.
5424    *
5425    * <p>The new array contains the same elements of the input
5426    * array plus the given element in the last position. The component type of
5427    * the new array is the same as that of the input array.
5428    *
5429    * <p>If the input array is {@code null}, a new one element array is returned
5430    * whose component type is the same as the element.
5431    *
5432    * <pre>
5433    * ArrayUtils.add(null, '0') = ['0']
5434    * ArrayUtils.add(['1'], '0') = ['1', '0']
5435    * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
5436    * </pre>
5437    *
5438    * @param array the array to copy and add the element to, may be {@code null}
5439    * @param element the object to add at the last index of the new array
5440    * @return A new array containing the existing elements plus the new element
5441    * @since 2.1
5442    */
 
5443  4 toggle public static char[] add(final char[] array, final char element) {
5444  4 final char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE);
5445  4 newArray[newArray.length - 1] = element;
5446  4 return newArray;
5447    }
5448   
5449    /**
5450    * <p>Copies the given array and adds the given element at the end of the new array.
5451    *
5452    * <p>The new array contains the same elements of the input
5453    * array plus the given element in the last position. The component type of
5454    * the new array is the same as that of the input array.
5455    *
5456    * <p>If the input array is {@code null}, a new one element array is returned
5457    * whose component type is the same as the element.
5458    *
5459    * <pre>
5460    * ArrayUtils.add(null, 0) = [0]
5461    * ArrayUtils.add([1], 0) = [1, 0]
5462    * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
5463    * </pre>
5464    *
5465    * @param array the array to copy and add the element to, may be {@code null}
5466    * @param element the object to add at the last index of the new array
5467    * @return A new array containing the existing elements plus the new element
5468    * @since 2.1
5469    */
 
5470  4 toggle public static double[] add(final double[] array, final double element) {
5471  4 final double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE);
5472  4 newArray[newArray.length - 1] = element;
5473  4 return newArray;
5474    }
5475   
5476    /**
5477    * <p>Copies the given array and adds the given element at the end of the new array.
5478    *
5479    * <p>The new array contains the same elements of the input
5480    * array plus the given element in the last position. The component type of
5481    * the new array is the same as that of the input array.
5482    *
5483    * <p>If the input array is {@code null}, a new one element array is returned
5484    * whose component type is the same as the element.
5485    *
5486    * <pre>
5487    * ArrayUtils.add(null, 0) = [0]
5488    * ArrayUtils.add([1], 0) = [1, 0]
5489    * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
5490    * </pre>
5491    *
5492    * @param array the array to copy and add the element to, may be {@code null}
5493    * @param element the object to add at the last index of the new array
5494    * @return A new array containing the existing elements plus the new element
5495    * @since 2.1
5496    */
 
5497  4 toggle public static float[] add(final float[] array, final float element) {
5498  4 final float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE);
5499  4 newArray[newArray.length - 1] = element;
5500  4 return newArray;
5501    }
5502   
5503    /**
5504    * <p>Copies the given array and adds the given element at the end of the new array.
5505    *
5506    * <p>The new array contains the same elements of the input
5507    * array plus the given element in the last position. The component type of
5508    * the new array is the same as that of the input array.
5509    *
5510    * <p>If the input array is {@code null}, a new one element array is returned
5511    * whose component type is the same as the element.
5512    *
5513    * <pre>
5514    * ArrayUtils.add(null, 0) = [0]
5515    * ArrayUtils.add([1], 0) = [1, 0]
5516    * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
5517    * </pre>
5518    *
5519    * @param array the array to copy and add the element to, may be {@code null}
5520    * @param element the object to add at the last index of the new array
5521    * @return A new array containing the existing elements plus the new element
5522    * @since 2.1
5523    */
 
5524  4 toggle public static int[] add(final int[] array, final int element) {
5525  4 final int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE);
5526  4 newArray[newArray.length - 1] = element;
5527  4 return newArray;
5528    }
5529   
5530    /**
5531    * <p>Copies the given array and adds the given element at the end of the new array.
5532    *
5533    * <p>The new array contains the same elements of the input
5534    * array plus the given element in the last position. The component type of
5535    * the new array is the same as that of the input array.
5536    *
5537    * <p>If the input array is {@code null}, a new one element array is returned
5538    * whose component type is the same as the element.
5539    *
5540    * <pre>
5541    * ArrayUtils.add(null, 0) = [0]
5542    * ArrayUtils.add([1], 0) = [1, 0]
5543    * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
5544    * </pre>
5545    *
5546    * @param array the array to copy and add the element to, may be {@code null}
5547    * @param element the object to add at the last index of the new array
5548    * @return A new array containing the existing elements plus the new element
5549    * @since 2.1
5550    */
 
5551  4 toggle public static long[] add(final long[] array, final long element) {
5552  4 final long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);
5553  4 newArray[newArray.length - 1] = element;
5554  4 return newArray;
5555    }
5556   
5557    /**
5558    * <p>Copies the given array and adds the given element at the end of the new array.
5559    *
5560    * <p>The new array contains the same elements of the input
5561    * array plus the given element in the last position. The component type of
5562    * the new array is the same as that of the input array.
5563    *
5564    * <p>If the input array is {@code null}, a new one element array is returned
5565    * whose component type is the same as the element.
5566    *
5567    * <pre>
5568    * ArrayUtils.add(null, 0) = [0]
5569    * ArrayUtils.add([1], 0) = [1, 0]
5570    * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
5571    * </pre>
5572    *
5573    * @param array the array to copy and add the element to, may be {@code null}
5574    * @param element the object to add at the last index of the new array
5575    * @return A new array containing the existing elements plus the new element
5576    * @since 2.1
5577    */
 
5578  4 toggle public static short[] add(final short[] array, final short element) {
5579  4 final short[] newArray = (short[]) copyArrayGrow1(array, Short.TYPE);
5580  4 newArray[newArray.length - 1] = element;
5581  4 return newArray;
5582    }
5583   
5584    /**
5585    * Returns a copy of the given array of size 1 greater than the argument.
5586    * The last value of the array is left to the default value.
5587    *
5588    * @param array The array to copy, must not be {@code null}.
5589    * @param newArrayComponentType If {@code array} is {@code null}, create a
5590    * size 1 array of this type.
5591    * @return A new copy of the array of size 1 greater than the input.
5592    */
 
5593  41 toggle private static Object copyArrayGrow1(final Object array, final Class<?> newArrayComponentType) {
5594  41 if (array != null) {
5595  22 final int arrayLength = Array.getLength(array);
5596  22 final Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
5597  22 System.arraycopy(array, 0, newArray, 0, arrayLength);
5598  22 return newArray;
5599    }
5600  19 return Array.newInstance(newArrayComponentType, 1);
5601    }
5602   
5603    /**
5604    * <p>Inserts the specified element at the specified position in the array.
5605    * Shifts the element currently at that position (if any) and any subsequent
5606    * elements to the right (adds one to their indices).
5607    *
5608    * <p>This method returns a new array with the same elements of the input
5609    * array plus the given element on the specified position. The component
5610    * type of the returned array is always the same as that of the input
5611    * array.
5612    *
5613    * <p>If the input array is {@code null}, a new one element array is returned
5614    * whose component type is the same as the element.
5615    *
5616    * <pre>
5617    * ArrayUtils.add(null, 0, null) = IllegalArgumentException
5618    * ArrayUtils.add(null, 0, "a") = ["a"]
5619    * ArrayUtils.add(["a"], 1, null) = ["a", null]
5620    * ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
5621    * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
5622    * </pre>
5623    *
5624    * @param <T> the component type of the array
5625    * @param array the array to add the element to, may be {@code null}
5626    * @param index the position of the new object
5627    * @param element the object to add
5628    * @return A new array containing the existing elements and the new element
5629    * @throws IndexOutOfBoundsException if the index is out of range (index &lt; 0 || index &gt; array.length).
5630    * @throws IllegalArgumentException if both array and element are null
5631    * @deprecated this method has been superseded by {@link #insert(int, Object[], Object...) insert(int, T[], T...)} and
5632    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5633    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5634    */
 
5635  8 toggle @Deprecated
5636    public static <T> T[] add(final T[] array, final int index, final T element) {
5637  8 Class<?> clss = null;
5638  8 if (array != null) {
5639  6 clss = array.getClass().getComponentType();
5640  2 } else if (element != null) {
5641  1 clss = element.getClass();
5642    } else {
5643  1 throw new IllegalArgumentException("Array and element cannot both be null");
5644    }
5645  7 @SuppressWarnings("unchecked") // the add method creates an array of type clss, which is type T
5646    final T[] newArray = (T[]) add(array, index, element, clss);
5647  7 return newArray;
5648    }
5649   
5650    /**
5651    * <p>Inserts the specified element at the specified position in the array.
5652    * Shifts the element currently at that position (if any) and any subsequent
5653    * elements to the right (adds one to their indices).
5654    *
5655    * <p>This method returns a new array with the same elements of the input
5656    * array plus the given element on the specified position. The component
5657    * type of the returned array is always the same as that of the input
5658    * array.
5659    *
5660    * <p>If the input array is {@code null}, a new one element array is returned
5661    * whose component type is the same as the element.
5662    *
5663    * <pre>
5664    * ArrayUtils.add(null, 0, true) = [true]
5665    * ArrayUtils.add([true], 0, false) = [false, true]
5666    * ArrayUtils.add([false], 1, true) = [false, true]
5667    * ArrayUtils.add([true, false], 1, true) = [true, true, false]
5668    * </pre>
5669    *
5670    * @param array the array to add the element to, may be {@code null}
5671    * @param index the position of the new object
5672    * @param element the object to add
5673    * @return A new array containing the existing elements and the new element
5674    * @throws IndexOutOfBoundsException if the index is out of range (index &lt; 0 || index &gt; array.length).
5675    * @deprecated this method has been superseded by {@link #insert(int, boolean[], boolean...)} and
5676    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5677    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5678    */
 
5679  7 toggle @Deprecated
5680    public static boolean[] add(final boolean[] array, final int index, final boolean element) {
5681  7 return (boolean[]) add(array, index, Boolean.valueOf(element), Boolean.TYPE);
5682    }
5683   
5684    /**
5685    * <p>Inserts the specified element at the specified position in the array.
5686    * Shifts the element currently at that position (if any) and any subsequent
5687    * elements to the right (adds one to their indices).
5688    *
5689    * <p>This method returns a new array with the same elements of the input
5690    * array plus the given element on the specified position. The component
5691    * type of the returned array is always the same as that of the input
5692    * array.
5693    *
5694    * <p>If the input array is {@code null}, a new one element array is returned
5695    * whose component type is the same as the element.
5696    *
5697    * <pre>
5698    * ArrayUtils.add(null, 0, 'a') = ['a']
5699    * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a']
5700    * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b']
5701    * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b']
5702    * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c']
5703    * </pre>
5704    *
5705    * @param array the array to add the element to, may be {@code null}
5706    * @param index the position of the new object
5707    * @param element the object to add
5708    * @return A new array containing the existing elements and the new element
5709    * @throws IndexOutOfBoundsException if the index is out of range
5710    * (index &lt; 0 || index &gt; array.length).
5711    * @deprecated this method has been superseded by {@link #insert(int, char[], char...)} and
5712    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5713    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5714    */
 
5715  8 toggle @Deprecated
5716    public static char[] add(final char[] array, final int index, final char element) {
5717  8 return (char[]) add(array, index, Character.valueOf(element), Character.TYPE);
5718    }
5719   
5720    /**
5721    * <p>Inserts the specified element at the specified position in the array.
5722    * Shifts the element currently at that position (if any) and any subsequent
5723    * elements to the right (adds one to their indices).
5724    *
5725    * <p>This method returns a new array with the same elements of the input
5726    * array plus the given element on the specified position. The component
5727    * type of the returned array is always the same as that of the input
5728    * array.
5729    *
5730    * <p>If the input array is {@code null}, a new one element array is returned
5731    * whose component type is the same as the element.
5732    *
5733    * <pre>
5734    * ArrayUtils.add([1], 0, 2) = [2, 1]
5735    * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3]
5736    * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6]
5737    * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
5738    * </pre>
5739    *
5740    * @param array the array to add the element to, may be {@code null}
5741    * @param index the position of the new object
5742    * @param element the object to add
5743    * @return A new array containing the existing elements and the new element
5744    * @throws IndexOutOfBoundsException if the index is out of range
5745    * (index &lt; 0 || index &gt; array.length).
5746    * @deprecated this method has been superseded by {@link #insert(int, byte[], byte...)} and
5747    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5748    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5749    */
 
5750  7 toggle @Deprecated
5751    public static byte[] add(final byte[] array, final int index, final byte element) {
5752  7 return (byte[]) add(array, index, Byte.valueOf(element), Byte.TYPE);
5753    }
5754   
5755    /**
5756    * <p>Inserts the specified element at the specified position in the array.
5757    * Shifts the element currently at that position (if any) and any subsequent
5758    * elements to the right (adds one to their indices).
5759    *
5760    * <p>This method returns a new array with the same elements of the input
5761    * array plus the given element on the specified position. The component
5762    * type of the returned array is always the same as that of the input
5763    * array.
5764    *
5765    * <p>If the input array is {@code null}, a new one element array is returned
5766    * whose component type is the same as the element.
5767    *
5768    * <pre>
5769    * ArrayUtils.add([1], 0, 2) = [2, 1]
5770    * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
5771    * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
5772    * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
5773    * </pre>
5774    *
5775    * @param array the array to add the element to, may be {@code null}
5776    * @param index the position of the new object
5777    * @param element the object to add
5778    * @return A new array containing the existing elements and the new element
5779    * @throws IndexOutOfBoundsException if the index is out of range
5780    * (index &lt; 0 || index &gt; array.length).
5781    * @deprecated this method has been superseded by {@link #insert(int, short[], short...)} and
5782    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5783    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5784    */
 
5785  7 toggle @Deprecated
5786    public static short[] add(final short[] array, final int index, final short element) {
5787  7 return (short[]) add(array, index, Short.valueOf(element), Short.TYPE);
5788    }
5789   
5790    /**
5791    * <p>Inserts the specified element at the specified position in the array.
5792    * Shifts the element currently at that position (if any) and any subsequent
5793    * elements to the right (adds one to their indices).
5794    *
5795    * <p>This method returns a new array with the same elements of the input
5796    * array plus the given element on the specified position. The component
5797    * type of the returned array is always the same as that of the input
5798    * array.
5799    *
5800    * <p>If the input array is {@code null}, a new one element array is returned
5801    * whose component type is the same as the element.
5802    *
5803    * <pre>
5804    * ArrayUtils.add([1], 0, 2) = [2, 1]
5805    * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
5806    * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
5807    * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
5808    * </pre>
5809    *
5810    * @param array the array to add the element to, may be {@code null}
5811    * @param index the position of the new object
5812    * @param element the object to add
5813    * @return A new array containing the existing elements and the new element
5814    * @throws IndexOutOfBoundsException if the index is out of range
5815    * (index &lt; 0 || index &gt; array.length).
5816    * @deprecated this method has been superseded by {@link #insert(int, int[], int...)} and
5817    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5818    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5819    */
 
5820  7 toggle @Deprecated
5821    public static int[] add(final int[] array, final int index, final int element) {
5822  7 return (int[]) add(array, index, Integer.valueOf(element), Integer.TYPE);
5823    }
5824   
5825    /**
5826    * <p>Inserts the specified element at the specified position in the array.
5827    * Shifts the element currently at that position (if any) and any subsequent
5828    * elements to the right (adds one to their indices).
5829    *
5830    * <p>This method returns a new array with the same elements of the input
5831    * array plus the given element on the specified position. The component
5832    * type of the returned array is always the same as that of the input
5833    * array.
5834    *
5835    * <p>If the input array is {@code null}, a new one element array is returned
5836    * whose component type is the same as the element.
5837    *
5838    * <pre>
5839    * ArrayUtils.add([1L], 0, 2L) = [2L, 1L]
5840    * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L]
5841    * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L]
5842    * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L]
5843    * </pre>
5844    *
5845    * @param array the array to add the element to, may be {@code null}
5846    * @param index the position of the new object
5847    * @param element the object to add
5848    * @return A new array containing the existing elements and the new element
5849    * @throws IndexOutOfBoundsException if the index is out of range
5850    * (index &lt; 0 || index &gt; array.length).
5851    * @deprecated this method has been superseded by {@link #insert(int, long[], long...)} and
5852    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5853    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5854    */
 
5855  7 toggle @Deprecated
5856    public static long[] add(final long[] array, final int index, final long element) {
5857  7 return (long[]) add(array, index, Long.valueOf(element), Long.TYPE);
5858    }
5859   
5860    /**
5861    * <p>Inserts the specified element at the specified position in the array.
5862    * Shifts the element currently at that position (if any) and any subsequent
5863    * elements to the right (adds one to their indices).
5864    *
5865    * <p>This method returns a new array with the same elements of the input
5866    * array plus the given element on the specified position. The component
5867    * type of the returned array is always the same as that of the input
5868    * array.
5869    *
5870    * <p>If the input array is {@code null}, a new one element array is returned
5871    * whose component type is the same as the element.
5872    *
5873    * <pre>
5874    * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f]
5875    * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f]
5876    * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f]
5877    * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f, 0.3f]
5878    * </pre>
5879    *
5880    * @param array the array to add the element to, may be {@code null}
5881    * @param index the position of the new object
5882    * @param element the object to add
5883    * @return A new array containing the existing elements and the new element
5884    * @throws IndexOutOfBoundsException if the index is out of range
5885    * (index &lt; 0 || index &gt; array.length).
5886    * @deprecated this method has been superseded by {@link #insert(int, float[], float...)} and
5887    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5888    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5889    */
 
5890  7 toggle @Deprecated
5891    public static float[] add(final float[] array, final int index, final float element) {
5892  7 return (float[]) add(array, index, Float.valueOf(element), Float.TYPE);
5893    }
5894   
5895    /**
5896    * <p>Inserts the specified element at the specified position in the array.
5897    * Shifts the element currently at that position (if any) and any subsequent
5898    * elements to the right (adds one to their indices).
5899    *
5900    * <p>This method returns a new array with the same elements of the input
5901    * array plus the given element on the specified position. The component
5902    * type of the returned array is always the same as that of the input
5903    * array.
5904    *
5905    * <p>If the input array is {@code null}, a new one element array is returned
5906    * whose component type is the same as the element.
5907    *
5908    * <pre>
5909    * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1]
5910    * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5]
5911    * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7]
5912    * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3]
5913    * </pre>
5914    *
5915    * @param array the array to add the element to, may be {@code null}
5916    * @param index the position of the new object
5917    * @param element the object to add
5918    * @return A new array containing the existing elements and the new element
5919    * @throws IndexOutOfBoundsException if the index is out of range
5920    * (index &lt; 0 || index &gt; array.length).
5921    * @deprecated this method has been superseded by {@link #insert(int, double[], double...)} and
5922    * may be removed in a future release. Please note the handling of {@code null} input arrays differs
5923    * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}.
5924    */
 
5925  7 toggle @Deprecated
5926    public static double[] add(final double[] array, final int index, final double element) {
5927  7 return (double[]) add(array, index, Double.valueOf(element), Double.TYPE);
5928    }
5929   
5930    /**
5931    * Underlying implementation of add(array, index, element) methods.
5932    * The last parameter is the class, which may not equal element.getClass
5933    * for primitives.
5934    *
5935    * @param array the array to add the element to, may be {@code null}
5936    * @param index the position of the new object
5937    * @param element the object to add
5938    * @param clss the type of the element being added
5939    * @return A new array containing the existing elements and the new element
5940    */
 
5941  64 toggle private static Object add(final Object array, final int index, final Object element, final Class<?> clss) {
5942  64 if (array == null) {
5943  11 if (index != 0) {
5944  8 throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0");
5945    }
5946  3 final Object joinedArray = Array.newInstance(clss, 1);
5947  3 Array.set(joinedArray, 0, element);
5948  3 return joinedArray;
5949    }
5950  53 final int length = Array.getLength(array);
5951  53 if (index > length || index < 0) {
5952  16 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
5953    }
5954  37 final Object result = Array.newInstance(clss, length + 1);
5955  37 System.arraycopy(array, 0, result, 0, index);
5956  37 Array.set(result, index, element);
5957  37 if (index < length) {
5958  27 System.arraycopy(array, index, result, index + 1, length - index);
5959    }
5960  37 return result;
5961    }
5962   
5963    /**
5964    * <p>Removes the element at the specified position from the specified array.
5965    * All subsequent elements are shifted to the left (subtracts one from
5966    * their indices).
5967    *
5968    * <p>This method returns a new array with the same elements of the input
5969    * array except the element on the specified position. The component
5970    * type of the returned array is always the same as that of the input
5971    * array.
5972    *
5973    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
5974    * will be thrown, because in that case no valid index can be specified.
5975    *
5976    * <pre>
5977    * ArrayUtils.remove(["a"], 0) = []
5978    * ArrayUtils.remove(["a", "b"], 0) = ["b"]
5979    * ArrayUtils.remove(["a", "b"], 1) = ["a"]
5980    * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
5981    * </pre>
5982    *
5983    * @param <T> the component type of the array
5984    * @param array the array to remove the element from, may not be {@code null}
5985    * @param index the position of the element to be removed
5986    * @return A new array containing the existing elements except the element
5987    * at the specified position.
5988    * @throws IndexOutOfBoundsException if the index is out of range
5989    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
5990    * @since 2.1
5991    */
 
5992  14 toggle @SuppressWarnings("unchecked") // remove() always creates an array of the same type as its input
5993    public static <T> T[] remove(final T[] array, final int index) {
5994  14 return (T[]) remove((Object) array, index);
5995    }
5996   
5997    /**
5998    * <p>Removes the first occurrence of the specified element from the
5999    * specified array. All subsequent elements are shifted to the left
6000    * (subtracts one from their indices). If the array doesn't contains
6001    * such an element, no elements are removed from the array.
6002    *
6003    * <p>This method returns a new array with the same elements of the input
6004    * array except the first occurrence of the specified element. The component
6005    * type of the returned array is always the same as that of the input
6006    * array.
6007    *
6008    * <pre>
6009    * ArrayUtils.removeElement(null, "a") = null
6010    * ArrayUtils.removeElement([], "a") = []
6011    * ArrayUtils.removeElement(["a"], "b") = ["a"]
6012    * ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
6013    * ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
6014    * </pre>
6015    *
6016    * @param <T> the component type of the array
6017    * @param array the array to remove the element from, may be {@code null}
6018    * @param element the element to be removed
6019    * @return A new array containing the existing elements except the first
6020    * occurrence of the specified element.
6021    * @since 2.1
6022    */
 
6023  5 toggle public static <T> T[] removeElement(final T[] array, final Object element) {
6024  5 final int index = indexOf(array, element);
6025  5 if (index == INDEX_NOT_FOUND) {
6026  2 return clone(array);
6027    }
6028  3 return remove(array, index);
6029    }
6030   
6031    /**
6032    * <p>Removes the element at the specified position from the specified array.
6033    * All subsequent elements are shifted to the left (subtracts one from
6034    * their indices).
6035    *
6036    * <p>This method returns a new array with the same elements of the input
6037    * array except the element on the specified position. The component
6038    * type of the returned array is always the same as that of the input
6039    * array.
6040    *
6041    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6042    * will be thrown, because in that case no valid index can be specified.
6043    *
6044    * <pre>
6045    * ArrayUtils.remove([true], 0) = []
6046    * ArrayUtils.remove([true, false], 0) = [false]
6047    * ArrayUtils.remove([true, false], 1) = [true]
6048    * ArrayUtils.remove([true, true, false], 1) = [true, false]
6049    * </pre>
6050    *
6051    * @param array the array to remove the element from, may not be {@code null}
6052    * @param index the position of the element to be removed
6053    * @return A new array containing the existing elements except the element
6054    * at the specified position.
6055    * @throws IndexOutOfBoundsException if the index is out of range
6056    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6057    * @since 2.1
6058    */
 
6059  10 toggle public static boolean[] remove(final boolean[] array, final int index) {
6060  10 return (boolean[]) remove((Object) array, index);
6061    }
6062   
6063    /**
6064    * <p>Removes the first occurrence of the specified element from the
6065    * specified array. All subsequent elements are shifted to the left
6066    * (subtracts one from their indices). If the array doesn't contains
6067    * such an element, no elements are removed from the array.
6068    *
6069    * <p>This method returns a new array with the same elements of the input
6070    * array except the first occurrence of the specified element. The component
6071    * type of the returned array is always the same as that of the input
6072    * array.
6073    *
6074    * <pre>
6075    * ArrayUtils.removeElement(null, true) = null
6076    * ArrayUtils.removeElement([], true) = []
6077    * ArrayUtils.removeElement([true], false) = [true]
6078    * ArrayUtils.removeElement([true, false], false) = [true]
6079    * ArrayUtils.removeElement([true, false, true], true) = [false, true]
6080    * </pre>
6081    *
6082    * @param array the array to remove the element from, may be {@code null}
6083    * @param element the element to be removed
6084    * @return A new array containing the existing elements except the first
6085    * occurrence of the specified element.
6086    * @since 2.1
6087    */
 
6088  5 toggle public static boolean[] removeElement(final boolean[] array, final boolean element) {
6089  5 final int index = indexOf(array, element);
6090  5 if (index == INDEX_NOT_FOUND) {
6091  2 return clone(array);
6092    }
6093  3 return remove(array, index);
6094    }
6095   
6096    /**
6097    * <p>Removes the element at the specified position from the specified array.
6098    * All subsequent elements are shifted to the left (subtracts one from
6099    * their indices).
6100    *
6101    * <p>This method returns a new array with the same elements of the input
6102    * array except the element on the specified position. The component
6103    * type of the returned array is always the same as that of the input
6104    * array.
6105    *
6106    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6107    * will be thrown, because in that case no valid index can be specified.
6108    *
6109    * <pre>
6110    * ArrayUtils.remove([1], 0) = []
6111    * ArrayUtils.remove([1, 0], 0) = [0]
6112    * ArrayUtils.remove([1, 0], 1) = [1]
6113    * ArrayUtils.remove([1, 0, 1], 1) = [1, 1]
6114    * </pre>
6115    *
6116    * @param array the array to remove the element from, may not be {@code null}
6117    * @param index the position of the element to be removed
6118    * @return A new array containing the existing elements except the element
6119    * at the specified position.
6120    * @throws IndexOutOfBoundsException if the index is out of range
6121    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6122    * @since 2.1
6123    */
 
6124  10 toggle public static byte[] remove(final byte[] array, final int index) {
6125  10 return (byte[]) remove((Object) array, index);
6126    }
6127   
6128    /**
6129    * <p>Removes the first occurrence of the specified element from the
6130    * specified array. All subsequent elements are shifted to the left
6131    * (subtracts one from their indices). If the array doesn't contains
6132    * such an element, no elements are removed from the array.
6133    *
6134    * <p>This method returns a new array with the same elements of the input
6135    * array except the first occurrence of the specified element. The component
6136    * type of the returned array is always the same as that of the input
6137    * array.
6138    *
6139    * <pre>
6140    * ArrayUtils.removeElement(null, 1) = null
6141    * ArrayUtils.removeElement([], 1) = []
6142    * ArrayUtils.removeElement([1], 0) = [1]
6143    * ArrayUtils.removeElement([1, 0], 0) = [1]
6144    * ArrayUtils.removeElement([1, 0, 1], 1) = [0, 1]
6145    * </pre>
6146    *
6147    * @param array the array to remove the element from, may be {@code null}
6148    * @param element the element to be removed
6149    * @return A new array containing the existing elements except the first
6150    * occurrence of the specified element.
6151    * @since 2.1
6152    */
 
6153  5 toggle public static byte[] removeElement(final byte[] array, final byte element) {
6154  5 final int index = indexOf(array, element);
6155  5 if (index == INDEX_NOT_FOUND) {
6156  2 return clone(array);
6157    }
6158  3 return remove(array, index);
6159    }
6160   
6161    /**
6162    * <p>Removes the element at the specified position from the specified array.
6163    * All subsequent elements are shifted to the left (subtracts one from
6164    * their indices).
6165    *
6166    * <p>This method returns a new array with the same elements of the input
6167    * array except the element on the specified position. The component
6168    * type of the returned array is always the same as that of the input
6169    * array.
6170    *
6171    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6172    * will be thrown, because in that case no valid index can be specified.
6173    *
6174    * <pre>
6175    * ArrayUtils.remove(['a'], 0) = []
6176    * ArrayUtils.remove(['a', 'b'], 0) = ['b']
6177    * ArrayUtils.remove(['a', 'b'], 1) = ['a']
6178    * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c']
6179    * </pre>
6180    *
6181    * @param array the array to remove the element from, may not be {@code null}
6182    * @param index the position of the element to be removed
6183    * @return A new array containing the existing elements except the element
6184    * at the specified position.
6185    * @throws IndexOutOfBoundsException if the index is out of range
6186    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6187    * @since 2.1
6188    */
 
6189  10 toggle public static char[] remove(final char[] array, final int index) {
6190  10 return (char[]) remove((Object) array, index);
6191    }
6192   
6193    /**
6194    * <p>Removes the first occurrence of the specified element from the
6195    * specified array. All subsequent elements are shifted to the left
6196    * (subtracts one from their indices). If the array doesn't contains
6197    * such an element, no elements are removed from the array.
6198    *
6199    * <p>This method returns a new array with the same elements of the input
6200    * array except the first occurrence of the specified element. The component
6201    * type of the returned array is always the same as that of the input
6202    * array.
6203    *
6204    * <pre>
6205    * ArrayUtils.removeElement(null, 'a') = null
6206    * ArrayUtils.removeElement([], 'a') = []
6207    * ArrayUtils.removeElement(['a'], 'b') = ['a']
6208    * ArrayUtils.removeElement(['a', 'b'], 'a') = ['b']
6209    * ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
6210    * </pre>
6211    *
6212    * @param array the array to remove the element from, may be {@code null}
6213    * @param element the element to be removed
6214    * @return A new array containing the existing elements except the first
6215    * occurrence of the specified element.
6216    * @since 2.1
6217    */
 
6218  5 toggle public static char[] removeElement(final char[] array, final char element) {
6219  5 final int index = indexOf(array, element);
6220  5 if (index == INDEX_NOT_FOUND) {
6221  2 return clone(array);
6222    }
6223  3 return remove(array, index);
6224    }
6225   
6226    /**
6227    * <p>Removes the element at the specified position from the specified array.
6228    * All subsequent elements are shifted to the left (subtracts one from
6229    * their indices).
6230    *
6231    * <p>This method returns a new array with the same elements of the input
6232    * array except the element on the specified position. The component
6233    * type of the returned array is always the same as that of the input
6234    * array.
6235    *
6236    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6237    * will be thrown, because in that case no valid index can be specified.
6238    *
6239    * <pre>
6240    * ArrayUtils.remove([1.1], 0) = []
6241    * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
6242    * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
6243    * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
6244    * </pre>
6245    *
6246    * @param array the array to remove the element from, may not be {@code null}
6247    * @param index the position of the element to be removed
6248    * @return A new array containing the existing elements except the element
6249    * at the specified position.
6250    * @throws IndexOutOfBoundsException if the index is out of range
6251    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6252    * @since 2.1
6253    */
 
6254  10 toggle public static double[] remove(final double[] array, final int index) {
6255  10 return (double[]) remove((Object) array, index);
6256    }
6257   
6258    /**
6259    * <p>Removes the first occurrence of the specified element from the
6260    * specified array. All subsequent elements are shifted to the left
6261    * (subtracts one from their indices). If the array doesn't contains
6262    * such an element, no elements are removed from the array.
6263    *
6264    * <p>This method returns a new array with the same elements of the input
6265    * array except the first occurrence of the specified element. The component
6266    * type of the returned array is always the same as that of the input
6267    * array.
6268    *
6269    * <pre>
6270    * ArrayUtils.removeElement(null, 1.1) = null
6271    * ArrayUtils.removeElement([], 1.1) = []
6272    * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
6273    * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
6274    * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
6275    * </pre>
6276    *
6277    * @param array the array to remove the element from, may be {@code null}
6278    * @param element the element to be removed
6279    * @return A new array containing the existing elements except the first
6280    * occurrence of the specified element.
6281    * @since 2.1
6282    */
 
6283  5 toggle public static double[] removeElement(final double[] array, final double element) {
6284  5 final int index = indexOf(array, element);
6285  5 if (index == INDEX_NOT_FOUND) {
6286  2 return clone(array);
6287    }
6288  3 return remove(array, index);
6289    }
6290   
6291    /**
6292    * <p>Removes the element at the specified position from the specified array.
6293    * All subsequent elements are shifted to the left (subtracts one from
6294    * their indices).
6295    *
6296    * <p>This method returns a new array with the same elements of the input
6297    * array except the element on the specified position. The component
6298    * type of the returned array is always the same as that of the input
6299    * array.
6300    *
6301    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6302    * will be thrown, because in that case no valid index can be specified.
6303    *
6304    * <pre>
6305    * ArrayUtils.remove([1.1], 0) = []
6306    * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
6307    * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
6308    * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
6309    * </pre>
6310    *
6311    * @param array the array to remove the element from, may not be {@code null}
6312    * @param index the position of the element to be removed
6313    * @return A new array containing the existing elements except the element
6314    * at the specified position.
6315    * @throws IndexOutOfBoundsException if the index is out of range
6316    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6317    * @since 2.1
6318    */
 
6319  10 toggle public static float[] remove(final float[] array, final int index) {
6320  10 return (float[]) remove((Object) array, index);
6321    }
6322   
6323    /**
6324    * <p>Removes the first occurrence of the specified element from the
6325    * specified array. All subsequent elements are shifted to the left
6326    * (subtracts one from their indices). If the array doesn't contains
6327    * such an element, no elements are removed from the array.
6328    *
6329    * <p>This method returns a new array with the same elements of the input
6330    * array except the first occurrence of the specified element. The component
6331    * type of the returned array is always the same as that of the input
6332    * array.
6333    *
6334    * <pre>
6335    * ArrayUtils.removeElement(null, 1.1) = null
6336    * ArrayUtils.removeElement([], 1.1) = []
6337    * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
6338    * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
6339    * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
6340    * </pre>
6341    *
6342    * @param array the array to remove the element from, may be {@code null}
6343    * @param element the element to be removed
6344    * @return A new array containing the existing elements except the first
6345    * occurrence of the specified element.
6346    * @since 2.1
6347    */
 
6348  5 toggle public static float[] removeElement(final float[] array, final float element) {
6349  5 final int index = indexOf(array, element);
6350  5 if (index == INDEX_NOT_FOUND) {
6351  2 return clone(array);
6352    }
6353  3 return remove(array, index);
6354    }
6355   
6356    /**
6357    * <p>Removes the element at the specified position from the specified array.
6358    * All subsequent elements are shifted to the left (subtracts one from
6359    * their indices).
6360    *
6361    * <p>This method returns a new array with the same elements of the input
6362    * array except the element on the specified position. The component
6363    * type of the returned array is always the same as that of the input
6364    * array.
6365    *
6366    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6367    * will be thrown, because in that case no valid index can be specified.
6368    *
6369    * <pre>
6370    * ArrayUtils.remove([1], 0) = []
6371    * ArrayUtils.remove([2, 6], 0) = [6]
6372    * ArrayUtils.remove([2, 6], 1) = [2]
6373    * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
6374    * </pre>
6375    *
6376    * @param array the array to remove the element from, may not be {@code null}
6377    * @param index the position of the element to be removed
6378    * @return A new array containing the existing elements except the element
6379    * at the specified position.
6380    * @throws IndexOutOfBoundsException if the index is out of range
6381    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6382    * @since 2.1
6383    */
 
6384  10 toggle public static int[] remove(final int[] array, final int index) {
6385  10 return (int[]) remove((Object) array, index);
6386    }
6387   
6388    /**
6389    * <p>Removes the first occurrence of the specified element from the
6390    * specified array. All subsequent elements are shifted to the left
6391    * (subtracts one from their indices). If the array doesn't contains
6392    * such an element, no elements are removed from the array.
6393    *
6394    * <p>This method returns a new array with the same elements of the input
6395    * array except the first occurrence of the specified element. The component
6396    * type of the returned array is always the same as that of the input
6397    * array.
6398    *
6399    * <pre>
6400    * ArrayUtils.removeElement(null, 1) = null
6401    * ArrayUtils.removeElement([], 1) = []
6402    * ArrayUtils.removeElement([1], 2) = [1]
6403    * ArrayUtils.removeElement([1, 3], 1) = [3]
6404    * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
6405    * </pre>
6406    *
6407    * @param array the array to remove the element from, may be {@code null}
6408    * @param element the element to be removed
6409    * @return A new array containing the existing elements except the first
6410    * occurrence of the specified element.
6411    * @since 2.1
6412    */
 
6413  5 toggle public static int[] removeElement(final int[] array, final int element) {
6414  5 final int index = indexOf(array, element);
6415  5 if (index == INDEX_NOT_FOUND) {
6416  2 return clone(array);
6417    }
6418  3 return remove(array, index);
6419    }
6420   
6421    /**
6422    * <p>Removes the element at the specified position from the specified array.
6423    * All subsequent elements are shifted to the left (subtracts one from
6424    * their indices).
6425    *
6426    * <p>This method returns a new array with the same elements of the input
6427    * array except the element on the specified position. The component
6428    * type of the returned array is always the same as that of the input
6429    * array.
6430    *
6431    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6432    * will be thrown, because in that case no valid index can be specified.
6433    *
6434    * <pre>
6435    * ArrayUtils.remove([1], 0) = []
6436    * ArrayUtils.remove([2, 6], 0) = [6]
6437    * ArrayUtils.remove([2, 6], 1) = [2]
6438    * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
6439    * </pre>
6440    *
6441    * @param array the array to remove the element from, may not be {@code null}
6442    * @param index the position of the element to be removed
6443    * @return A new array containing the existing elements except the element
6444    * at the specified position.
6445    * @throws IndexOutOfBoundsException if the index is out of range
6446    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6447    * @since 2.1
6448    */
 
6449  10 toggle public static long[] remove(final long[] array, final int index) {
6450  10 return (long[]) remove((Object) array, index);
6451    }
6452   
6453    /**
6454    * <p>Removes the first occurrence of the specified element from the
6455    * specified array. All subsequent elements are shifted to the left
6456    * (subtracts one from their indices). If the array doesn't contains
6457    * such an element, no elements are removed from the array.
6458    *
6459    * <p>This method returns a new array with the same elements of the input
6460    * array except the first occurrence of the specified element. The component
6461    * type of the returned array is always the same as that of the input
6462    * array.
6463    *
6464    * <pre>
6465    * ArrayUtils.removeElement(null, 1) = null
6466    * ArrayUtils.removeElement([], 1) = []
6467    * ArrayUtils.removeElement([1], 2) = [1]
6468    * ArrayUtils.removeElement([1, 3], 1) = [3]
6469    * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
6470    * </pre>
6471    *
6472    * @param array the array to remove the element from, may be {@code null}
6473    * @param element the element to be removed
6474    * @return A new array containing the existing elements except the first
6475    * occurrence of the specified element.
6476    * @since 2.1
6477    */
 
6478  5 toggle public static long[] removeElement(final long[] array, final long element) {
6479  5 final int index = indexOf(array, element);
6480  5 if (index == INDEX_NOT_FOUND) {
6481  2 return clone(array);
6482    }
6483  3 return remove(array, index);
6484    }
6485   
6486    /**
6487    * <p>Removes the element at the specified position from the specified array.
6488    * All subsequent elements are shifted to the left (subtracts one from
6489    * their indices).
6490    *
6491    * <p>This method returns a new array with the same elements of the input
6492    * array except the element on the specified position. The component
6493    * type of the returned array is always the same as that of the input
6494    * array.
6495    *
6496    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6497    * will be thrown, because in that case no valid index can be specified.
6498    *
6499    * <pre>
6500    * ArrayUtils.remove([1], 0) = []
6501    * ArrayUtils.remove([2, 6], 0) = [6]
6502    * ArrayUtils.remove([2, 6], 1) = [2]
6503    * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
6504    * </pre>
6505    *
6506    * @param array the array to remove the element from, may not be {@code null}
6507    * @param index the position of the element to be removed
6508    * @return A new array containing the existing elements except the element
6509    * at the specified position.
6510    * @throws IndexOutOfBoundsException if the index is out of range
6511    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6512    * @since 2.1
6513    */
 
6514  10 toggle public static short[] remove(final short[] array, final int index) {
6515  10 return (short[]) remove((Object) array, index);
6516    }
6517   
6518    /**
6519    * <p>Removes the first occurrence of the specified element from the
6520    * specified array. All subsequent elements are shifted to the left
6521    * (subtracts one from their indices). If the array doesn't contains
6522    * such an element, no elements are removed from the array.
6523    *
6524    * <p>This method returns a new array with the same elements of the input
6525    * array except the first occurrence of the specified element. The component
6526    * type of the returned array is always the same as that of the input
6527    * array.
6528    *
6529    * <pre>
6530    * ArrayUtils.removeElement(null, 1) = null
6531    * ArrayUtils.removeElement([], 1) = []
6532    * ArrayUtils.removeElement([1], 2) = [1]
6533    * ArrayUtils.removeElement([1, 3], 1) = [3]
6534    * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
6535    * </pre>
6536    *
6537    * @param array the array to remove the element from, may be {@code null}
6538    * @param element the element to be removed
6539    * @return A new array containing the existing elements except the first
6540    * occurrence of the specified element.
6541    * @since 2.1
6542    */
 
6543  5 toggle public static short[] removeElement(final short[] array, final short element) {
6544  5 final int index = indexOf(array, element);
6545  5 if (index == INDEX_NOT_FOUND) {
6546  2 return clone(array);
6547    }
6548  3 return remove(array, index);
6549    }
6550   
6551    /**
6552    * <p>Removes the element at the specified position from the specified array.
6553    * All subsequent elements are shifted to the left (subtracts one from
6554    * their indices).
6555    *
6556    * <p>This method returns a new array with the same elements of the input
6557    * array except the element on the specified position. The component
6558    * type of the returned array is always the same as that of the input
6559    * array.
6560    *
6561    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6562    * will be thrown, because in that case no valid index can be specified.
6563    *
6564    * @param array the array to remove the element from, may not be {@code null}
6565    * @param index the position of the element to be removed
6566    * @return A new array containing the existing elements except the element
6567    * at the specified position.
6568    * @throws IndexOutOfBoundsException if the index is out of range
6569    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6570    * @since 2.1
6571    */
 
6572  94 toggle private static Object remove(final Object array, final int index) {
6573  94 final int length = getLength(array);
6574  94 if (index < 0 || index >= length) {
6575  28 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
6576    }
6577   
6578  66 final Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
6579  66 System.arraycopy(array, 0, result, 0, index);
6580  66 if (index < length - 1) {
6581  37 System.arraycopy(array, index + 1, result, index, length - index - 1);
6582    }
6583   
6584  66 return result;
6585    }
6586   
6587    /**
6588    * <p>Removes the elements at the specified positions from the specified array.
6589    * All remaining elements are shifted to the left.
6590    *
6591    * <p>This method returns a new array with the same elements of the input
6592    * array except those at the specified positions. The component
6593    * type of the returned array is always the same as that of the input
6594    * array.
6595    *
6596    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6597    * will be thrown, because in that case no valid index can be specified.
6598    *
6599    * <pre>
6600    * ArrayUtils.removeAll(["a", "b", "c"], 0, 2) = ["b"]
6601    * ArrayUtils.removeAll(["a", "b", "c"], 1, 2) = ["a"]
6602    * </pre>
6603    *
6604    * @param <T> the component type of the array
6605    * @param array the array to remove the element from, may not be {@code null}
6606    * @param indices the positions of the elements to be removed
6607    * @return A new array containing the existing elements except those
6608    * at the specified positions.
6609    * @throws IndexOutOfBoundsException if any index is out of range
6610    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6611    * @since 3.0.1
6612    */
 
6613  23 toggle @SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input
6614    public static <T> T[] removeAll(final T[] array, final int... indices) {
6615  23 return (T[]) removeAll((Object) array, indices);
6616    }
6617   
6618    /**
6619    * <p>Removes occurrences of specified elements, in specified quantities,
6620    * from the specified array. All subsequent elements are shifted left.
6621    * For any element-to-be-removed specified in greater quantities than
6622    * contained in the original array, no change occurs beyond the
6623    * removal of the existing matching items.
6624    *
6625    * <p>This method returns a new array with the same elements of the input
6626    * array except for the earliest-encountered occurrences of the specified
6627    * elements. The component type of the returned array is always the same
6628    * as that of the input array.
6629    *
6630    * <pre>
6631    * ArrayUtils.removeElements(null, "a", "b") = null
6632    * ArrayUtils.removeElements([], "a", "b") = []
6633    * ArrayUtils.removeElements(["a"], "b", "c") = ["a"]
6634    * ArrayUtils.removeElements(["a", "b"], "a", "c") = ["b"]
6635    * ArrayUtils.removeElements(["a", "b", "a"], "a") = ["b", "a"]
6636    * ArrayUtils.removeElements(["a", "b", "a"], "a", "a") = ["b"]
6637    * </pre>
6638    *
6639    * @param <T> the component type of the array
6640    * @param array the array to remove the element from, may be {@code null}
6641    * @param values the elements to be removed
6642    * @return A new array containing the existing elements except the
6643    * earliest-encountered occurrences of the specified elements.
6644    * @since 3.0.1
6645    */
 
6646  13 toggle @SafeVarargs
6647    public static <T> T[] removeElements(final T[] array, final T... values) {
6648  13 if (isEmpty(array) || isEmpty(values)) {
6649  4 return clone(array);
6650    }
6651  9 final HashMap<T, MutableInt> occurrences = new HashMap<>(values.length);
6652  9 for (final T v : values) {
6653  16 final MutableInt count = occurrences.get(v);
6654  16 if (count == null) {
6655  12 occurrences.put(v, new MutableInt(1));
6656    } else {
6657  4 count.increment();
6658    }
6659    }
6660  9 final BitSet toRemove = new BitSet();
6661  30 for (int i = 0; i < array.length; i++) {
6662  21 final T key = array[i];
6663  21 final MutableInt count = occurrences.get(key);
6664  21 if (count != null) {
6665  12 if (count.decrementAndGet() == 0) {
6666  9 occurrences.remove(key);
6667    }
6668  12 toRemove.set(i);
6669    }
6670    }
6671  9 @SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input
6672    final T[] result = (T[]) removeAll(array, toRemove);
6673  9 return result;
6674    }
6675   
6676    /**
6677    * <p>Removes the elements at the specified positions from the specified array.
6678    * All remaining elements are shifted to the left.
6679    *
6680    * <p>This method returns a new array with the same elements of the input
6681    * array except those at the specified positions. The component
6682    * type of the returned array is always the same as that of the input
6683    * array.
6684    *
6685    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6686    * will be thrown, because in that case no valid index can be specified.
6687    *
6688    * <pre>
6689    * ArrayUtils.removeAll([1], 0) = []
6690    * ArrayUtils.removeAll([2, 6], 0) = [6]
6691    * ArrayUtils.removeAll([2, 6], 0, 1) = []
6692    * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
6693    * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
6694    * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
6695    * </pre>
6696    *
6697    * @param array the array to remove the element from, may not be {@code null}
6698    * @param indices the positions of the elements to be removed
6699    * @return A new array containing the existing elements except those
6700    * at the specified positions.
6701    * @throws IndexOutOfBoundsException if any index is out of range
6702    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6703    * @since 3.0.1
6704    */
 
6705  19 toggle public static byte[] removeAll(final byte[] array, final int... indices) {
6706  19 return (byte[]) removeAll((Object) array, indices);
6707    }
6708   
6709    /**
6710    * <p>Removes occurrences of specified elements, in specified quantities,
6711    * from the specified array. All subsequent elements are shifted left.
6712    * For any element-to-be-removed specified in greater quantities than
6713    * contained in the original array, no change occurs beyond the
6714    * removal of the existing matching items.
6715    *
6716    * <p>This method returns a new array with the same elements of the input
6717    * array except for the earliest-encountered occurrences of the specified
6718    * elements. The component type of the returned array is always the same
6719    * as that of the input array.
6720    *
6721    * <pre>
6722    * ArrayUtils.removeElements(null, 1, 2) = null
6723    * ArrayUtils.removeElements([], 1, 2) = []
6724    * ArrayUtils.removeElements([1], 2, 3) = [1]
6725    * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
6726    * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
6727    * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
6728    * </pre>
6729    *
6730    * @param array the array to remove the element from, may be {@code null}
6731    * @param values the elements to be removed
6732    * @return A new array containing the existing elements except the
6733    * earliest-encountered occurrences of the specified elements.
6734    * @since 3.0.1
6735    */
 
6736  13 toggle public static byte[] removeElements(final byte[] array, final byte... values) {
6737  13 if (isEmpty(array) || isEmpty(values)) {
6738  4 return clone(array);
6739    }
6740  9 final Map<Byte, MutableInt> occurrences = new HashMap<>(values.length);
6741  9 for (final byte v : values) {
6742  17 final Byte boxed = Byte.valueOf(v);
6743  17 final MutableInt count = occurrences.get(boxed);
6744  17 if (count == null) {
6745  12 occurrences.put(boxed, new MutableInt(1));
6746    } else {
6747  5 count.increment();
6748    }
6749    }
6750  9 final BitSet toRemove = new BitSet();
6751  29 for (int i = 0; i < array.length; i++) {
6752  20 final byte key = array[i];
6753  20 final MutableInt count = occurrences.get(key);
6754  20 if (count != null) {
6755  13 if (count.decrementAndGet() == 0) {
6756  9 occurrences.remove(key);
6757    }
6758  13 toRemove.set(i);
6759    }
6760    }
6761  9 return (byte[]) removeAll(array, toRemove);
6762    }
6763   
6764    /**
6765    * <p>Removes the elements at the specified positions from the specified array.
6766    * All remaining elements are shifted to the left.
6767    *
6768    * <p>This method returns a new array with the same elements of the input
6769    * array except those at the specified positions. The component
6770    * type of the returned array is always the same as that of the input
6771    * array.
6772    *
6773    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6774    * will be thrown, because in that case no valid index can be specified.
6775    *
6776    * <pre>
6777    * ArrayUtils.removeAll([1], 0) = []
6778    * ArrayUtils.removeAll([2, 6], 0) = [6]
6779    * ArrayUtils.removeAll([2, 6], 0, 1) = []
6780    * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
6781    * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
6782    * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
6783    * </pre>
6784    *
6785    * @param array the array to remove the element from, may not be {@code null}
6786    * @param indices the positions of the elements to be removed
6787    * @return A new array containing the existing elements except those
6788    * at the specified positions.
6789    * @throws IndexOutOfBoundsException if any index is out of range
6790    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6791    * @since 3.0.1
6792    */
 
6793  19 toggle public static short[] removeAll(final short[] array, final int... indices) {
6794  19 return (short[]) removeAll((Object) array, indices);
6795    }
6796   
6797    /**
6798    * <p>Removes occurrences of specified elements, in specified quantities,
6799    * from the specified array. All subsequent elements are shifted left.
6800    * For any element-to-be-removed specified in greater quantities than
6801    * contained in the original array, no change occurs beyond the
6802    * removal of the existing matching items.
6803    *
6804    * <p>This method returns a new array with the same elements of the input
6805    * array except for the earliest-encountered occurrences of the specified
6806    * elements. The component type of the returned array is always the same
6807    * as that of the input array.
6808    *
6809    * <pre>
6810    * ArrayUtils.removeElements(null, 1, 2) = null
6811    * ArrayUtils.removeElements([], 1, 2) = []
6812    * ArrayUtils.removeElements([1], 2, 3) = [1]
6813    * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
6814    * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
6815    * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
6816    * </pre>
6817    *
6818    * @param array the array to remove the element from, may be {@code null}
6819    * @param values the elements to be removed
6820    * @return A new array containing the existing elements except the
6821    * earliest-encountered occurrences of the specified elements.
6822    * @since 3.0.1
6823    */
 
6824  13 toggle public static short[] removeElements(final short[] array, final short... values) {
6825  13 if (isEmpty(array) || isEmpty(values)) {
6826  4 return clone(array);
6827    }
6828  9 final HashMap<Short, MutableInt> occurrences = new HashMap<>(values.length);
6829  9 for (final short v : values) {
6830  17 final Short boxed = Short.valueOf(v);
6831  17 final MutableInt count = occurrences.get(boxed);
6832  17 if (count == null) {
6833  11 occurrences.put(boxed, new MutableInt(1));
6834    } else {
6835  6 count.increment();
6836    }
6837    }
6838  9 final BitSet toRemove = new BitSet();
6839  29 for (int i = 0; i < array.length; i++) {
6840  20 final short key = array[i];
6841  20 final MutableInt count = occurrences.get(key);
6842  20 if (count != null) {
6843  13 if (count.decrementAndGet() == 0) {
6844  8 occurrences.remove(key);
6845    }
6846  13 toRemove.set(i);
6847    }
6848    }
6849  9 return (short[]) removeAll(array, toRemove);
6850    }
6851   
6852    /**
6853    * <p>Removes the elements at the specified positions from the specified array.
6854    * All remaining elements are shifted to the left.
6855    *
6856    * <p>This method returns a new array with the same elements of the input
6857    * array except those at the specified positions. The component
6858    * type of the returned array is always the same as that of the input
6859    * array.
6860    *
6861    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6862    * will be thrown, because in that case no valid index can be specified.
6863    *
6864    * <pre>
6865    * ArrayUtils.removeAll([1], 0) = []
6866    * ArrayUtils.removeAll([2, 6], 0) = [6]
6867    * ArrayUtils.removeAll([2, 6], 0, 1) = []
6868    * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
6869    * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
6870    * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
6871    * </pre>
6872    *
6873    * @param array the array to remove the element from, may not be {@code null}
6874    * @param indices the positions of the elements to be removed
6875    * @return A new array containing the existing elements except those
6876    * at the specified positions.
6877    * @throws IndexOutOfBoundsException if any index is out of range
6878    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6879    * @since 3.0.1
6880    */
 
6881  21 toggle public static int[] removeAll(final int[] array, final int... indices) {
6882  21 return (int[]) removeAll((Object) array, indices);
6883    }
6884   
6885    /**
6886    * <p>Removes occurrences of specified elements, in specified quantities,
6887    * from the specified array. All subsequent elements are shifted left.
6888    * For any element-to-be-removed specified in greater quantities than
6889    * contained in the original array, no change occurs beyond the
6890    * removal of the existing matching items.
6891    *
6892    * <p>This method returns a new array with the same elements of the input
6893    * array except for the earliest-encountered occurrences of the specified
6894    * elements. The component type of the returned array is always the same
6895    * as that of the input array.
6896    *
6897    * <pre>
6898    * ArrayUtils.removeElements(null, 1, 2) = null
6899    * ArrayUtils.removeElements([], 1, 2) = []
6900    * ArrayUtils.removeElements([1], 2, 3) = [1]
6901    * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
6902    * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
6903    * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
6904    * </pre>
6905    *
6906    * @param array the array to remove the element from, may be {@code null}
6907    * @param values the elements to be removed
6908    * @return A new array containing the existing elements except the
6909    * earliest-encountered occurrences of the specified elements.
6910    * @since 3.0.1
6911    */
 
6912  13 toggle public static int[] removeElements(final int[] array, final int... values) {
6913  13 if (isEmpty(array) || isEmpty(values)) {
6914  4 return clone(array);
6915    }
6916  9 final HashMap<Integer, MutableInt> occurrences = new HashMap<>(values.length);
6917  9 for (final int v : values) {
6918  17 final Integer boxed = Integer.valueOf(v);
6919  17 final MutableInt count = occurrences.get(boxed);
6920  17 if (count == null) {
6921  11 occurrences.put(boxed, new MutableInt(1));
6922    } else {
6923  6 count.increment();
6924    }
6925    }
6926  9 final BitSet toRemove = new BitSet();
6927  29 for (int i = 0; i < array.length; i++) {
6928  20 final int key = array[i];
6929  20 final MutableInt count = occurrences.get(key);
6930  20 if (count != null) {
6931  13 if (count.decrementAndGet() == 0) {
6932  8 occurrences.remove(key);
6933    }
6934  13 toRemove.set(i);
6935    }
6936    }
6937  9 return (int[]) removeAll(array, toRemove);
6938    }
6939   
6940    /**
6941    * <p>Removes the elements at the specified positions from the specified array.
6942    * All remaining elements are shifted to the left.
6943    *
6944    * <p>This method returns a new array with the same elements of the input
6945    * array except those at the specified positions. The component
6946    * type of the returned array is always the same as that of the input
6947    * array.
6948    *
6949    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
6950    * will be thrown, because in that case no valid index can be specified.
6951    *
6952    * <pre>
6953    * ArrayUtils.removeAll([1], 0) = []
6954    * ArrayUtils.removeAll([2, 6], 0) = [6]
6955    * ArrayUtils.removeAll([2, 6], 0, 1) = []
6956    * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
6957    * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
6958    * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
6959    * </pre>
6960    *
6961    * @param array the array to remove the element from, may not be {@code null}
6962    * @param indices the positions of the elements to be removed
6963    * @return A new array containing the existing elements except those
6964    * at the specified positions.
6965    * @throws IndexOutOfBoundsException if any index is out of range
6966    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
6967    * @since 3.0.1
6968    */
 
6969  19 toggle public static char[] removeAll(final char[] array, final int... indices) {
6970  19 return (char[]) removeAll((Object) array, indices);
6971    }
6972   
6973    /**
6974    * <p>Removes occurrences of specified elements, in specified quantities,
6975    * from the specified array. All subsequent elements are shifted left.
6976    * For any element-to-be-removed specified in greater quantities than
6977    * contained in the original array, no change occurs beyond the
6978    * removal of the existing matching items.
6979    *
6980    * <p>This method returns a new array with the same elements of the input
6981    * array except for the earliest-encountered occurrences of the specified
6982    * elements. The component type of the returned array is always the same
6983    * as that of the input array.
6984    *
6985    * <pre>
6986    * ArrayUtils.removeElements(null, 1, 2) = null
6987    * ArrayUtils.removeElements([], 1, 2) = []
6988    * ArrayUtils.removeElements([1], 2, 3) = [1]
6989    * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
6990    * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
6991    * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
6992    * </pre>
6993    *
6994    * @param array the array to remove the element from, may be {@code null}
6995    * @param values the elements to be removed
6996    * @return A new array containing the existing elements except the
6997    * earliest-encountered occurrences of the specified elements.
6998    * @since 3.0.1
6999    */
 
7000  13 toggle public static char[] removeElements(final char[] array, final char... values) {
7001  13 if (isEmpty(array) || isEmpty(values)) {
7002  4 return clone(array);
7003    }
7004  9 final HashMap<Character, MutableInt> occurrences = new HashMap<>(values.length);
7005  9 for (final char v : values) {
7006  17 final Character boxed = Character.valueOf(v);
7007  17 final MutableInt count = occurrences.get(boxed);
7008  17 if (count == null) {
7009  12 occurrences.put(boxed, new MutableInt(1));
7010    } else {
7011  5 count.increment();
7012    }
7013    }
7014  9 final BitSet toRemove = new BitSet();
7015  29 for (int i = 0; i < array.length; i++) {
7016  20 final char key = array[i];
7017  20 final MutableInt count = occurrences.get(key);
7018  20 if (count != null) {
7019  13 if (count.decrementAndGet() == 0) {
7020  9 occurrences.remove(key);
7021    }
7022  13 toRemove.set(i);
7023    }
7024    }
7025  9 return (char[]) removeAll(array, toRemove);
7026    }
7027   
7028    /**
7029    * <p>Removes the elements at the specified positions from the specified array.
7030    * All remaining elements are shifted to the left.
7031    *
7032    * <p>This method returns a new array with the same elements of the input
7033    * array except those at the specified positions. The component
7034    * type of the returned array is always the same as that of the input
7035    * array.
7036    *
7037    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
7038    * will be thrown, because in that case no valid index can be specified.
7039    *
7040    * <pre>
7041    * ArrayUtils.removeAll([1], 0) = []
7042    * ArrayUtils.removeAll([2, 6], 0) = [6]
7043    * ArrayUtils.removeAll([2, 6], 0, 1) = []
7044    * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
7045    * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
7046    * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
7047    * </pre>
7048    *
7049    * @param array the array to remove the element from, may not be {@code null}
7050    * @param indices the positions of the elements to be removed
7051    * @return A new array containing the existing elements except those
7052    * at the specified positions.
7053    * @throws IndexOutOfBoundsException if any index is out of range
7054    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
7055    * @since 3.0.1
7056    */
 
7057  19 toggle public static long[] removeAll(final long[] array, final int... indices) {
7058  19 return (long[]) removeAll((Object) array, indices);
7059    }
7060   
7061    /**
7062    * <p>Removes occurrences of specified elements, in specified quantities,
7063    * from the specified array. All subsequent elements are shifted left.
7064    * For any element-to-be-removed specified in greater quantities than
7065    * contained in the original array, no change occurs beyond the
7066    * removal of the existing matching items.
7067    *
7068    * <p>This method returns a new array with the same elements of the input
7069    * array except for the earliest-encountered occurrences of the specified
7070    * elements. The component type of the returned array is always the same
7071    * as that of the input array.
7072    *
7073    * <pre>
7074    * ArrayUtils.removeElements(null, 1, 2) = null
7075    * ArrayUtils.removeElements([], 1, 2) = []
7076    * ArrayUtils.removeElements([1], 2, 3) = [1]
7077    * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
7078    * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
7079    * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
7080    * </pre>
7081    *
7082    * @param array the array to remove the element from, may be {@code null}
7083    * @param values the elements to be removed
7084    * @return A new array containing the existing elements except the
7085    * earliest-encountered occurrences of the specified elements.
7086    * @since 3.0.1
7087    */
 
7088  13 toggle public static long[] removeElements(final long[] array, final long... values) {
7089  13 if (isEmpty(array) || isEmpty(values)) {
7090  4 return clone(array);
7091    }
7092  9 final HashMap<Long, MutableInt> occurrences = new HashMap<>(values.length);
7093  9 for (final long v : values) {
7094  17 final Long boxed = Long.valueOf(v);
7095  17 final MutableInt count = occurrences.get(boxed);
7096  17 if (count == null) {
7097  11 occurrences.put(boxed, new MutableInt(1));
7098    } else {
7099  6 count.increment();
7100    }
7101    }
7102  9 final BitSet toRemove = new BitSet();
7103  29 for (int i = 0; i < array.length; i++) {
7104  20 final long key = array[i];
7105  20 final MutableInt count = occurrences.get(key);
7106  20 if (count != null) {
7107  13 if (count.decrementAndGet() == 0) {
7108  8 occurrences.remove(key);
7109    }
7110  13 toRemove.set(i);
7111    }
7112    }
7113  9 return (long[]) removeAll(array, toRemove);
7114    }
7115   
7116    /**
7117    * <p>Removes the elements at the specified positions from the specified array.
7118    * All remaining elements are shifted to the left.
7119    *
7120    * <p>This method returns a new array with the same elements of the input
7121    * array except those at the specified positions. The component
7122    * type of the returned array is always the same as that of the input
7123    * array.
7124    *
7125    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
7126    * will be thrown, because in that case no valid index can be specified.
7127    *
7128    * <pre>
7129    * ArrayUtils.removeAll([1], 0) = []
7130    * ArrayUtils.removeAll([2, 6], 0) = [6]
7131    * ArrayUtils.removeAll([2, 6], 0, 1) = []
7132    * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
7133    * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
7134    * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
7135    * </pre>
7136    *
7137    * @param array the array to remove the element from, may not be {@code null}
7138    * @param indices the positions of the elements to be removed
7139    * @return A new array containing the existing elements except those
7140    * at the specified positions.
7141    * @throws IndexOutOfBoundsException if any index is out of range
7142    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
7143    * @since 3.0.1
7144    */
 
7145  19 toggle public static float[] removeAll(final float[] array, final int... indices) {
7146  19 return (float[]) removeAll((Object) array, indices);
7147    }
7148   
7149    /**
7150    * <p>Removes occurrences of specified elements, in specified quantities,
7151    * from the specified array. All subsequent elements are shifted left.
7152    * For any element-to-be-removed specified in greater quantities than
7153    * contained in the original array, no change occurs beyond the
7154    * removal of the existing matching items.
7155    *
7156    * <p>This method returns a new array with the same elements of the input
7157    * array except for the earliest-encountered occurrences of the specified
7158    * elements. The component type of the returned array is always the same
7159    * as that of the input array.
7160    *
7161    * <pre>
7162    * ArrayUtils.removeElements(null, 1, 2) = null
7163    * ArrayUtils.removeElements([], 1, 2) = []
7164    * ArrayUtils.removeElements([1], 2, 3) = [1]
7165    * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
7166    * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
7167    * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
7168    * </pre>
7169    *
7170    * @param array the array to remove the element from, may be {@code null}
7171    * @param values the elements to be removed
7172    * @return A new array containing the existing elements except the
7173    * earliest-encountered occurrences of the specified elements.
7174    * @since 3.0.1
7175    */
 
7176  13 toggle public static float[] removeElements(final float[] array, final float... values) {
7177  13 if (isEmpty(array) || isEmpty(values)) {
7178  4 return clone(array);
7179    }
7180  9 final HashMap<Float, MutableInt> occurrences = new HashMap<>(values.length);
7181  9 for (final float v : values) {
7182  17 final Float boxed = Float.valueOf(v);
7183  17 final MutableInt count = occurrences.get(boxed);
7184  17 if (count == null) {
7185  11 occurrences.put(boxed, new MutableInt(1));
7186    } else {
7187  6 count.increment();
7188    }
7189    }
7190  9 final BitSet toRemove = new BitSet();
7191  29 for (int i = 0; i < array.length; i++) {
7192  20 final float key = array[i];
7193  20 final MutableInt count = occurrences.get(key);
7194  20 if (count != null) {
7195  13 if (count.decrementAndGet() == 0) {
7196  8 occurrences.remove(key);
7197    }
7198  13 toRemove.set(i);
7199    }
7200    }
7201  9 return (float[]) removeAll(array, toRemove);
7202    }
7203   
7204    /**
7205    * <p>Removes the elements at the specified positions from the specified array.
7206    * All remaining elements are shifted to the left.
7207    *
7208    * <p>This method returns a new array with the same elements of the input
7209    * array except those at the specified positions. The component
7210    * type of the returned array is always the same as that of the input
7211    * array.
7212    *
7213    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
7214    * will be thrown, because in that case no valid index can be specified.
7215    *
7216    * <pre>
7217    * ArrayUtils.removeAll([1], 0) = []
7218    * ArrayUtils.removeAll([2, 6], 0) = [6]
7219    * ArrayUtils.removeAll([2, 6], 0, 1) = []
7220    * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
7221    * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
7222    * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
7223    * </pre>
7224    *
7225    * @param array the array to remove the element from, may not be {@code null}
7226    * @param indices the positions of the elements to be removed
7227    * @return A new array containing the existing elements except those
7228    * at the specified positions.
7229    * @throws IndexOutOfBoundsException if any index is out of range
7230    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
7231    * @since 3.0.1
7232    */
 
7233  19 toggle public static double[] removeAll(final double[] array, final int... indices) {
7234  19 return (double[]) removeAll((Object) array, indices);
7235    }
7236   
7237    /**
7238    * <p>Removes occurrences of specified elements, in specified quantities,
7239    * from the specified array. All subsequent elements are shifted left.
7240    * For any element-to-be-removed specified in greater quantities than
7241    * contained in the original array, no change occurs beyond the
7242    * removal of the existing matching items.
7243    *
7244    * <p>This method returns a new array with the same elements of the input
7245    * array except for the earliest-encountered occurrences of the specified
7246    * elements. The component type of the returned array is always the same
7247    * as that of the input array.
7248    *
7249    * <pre>
7250    * ArrayUtils.removeElements(null, 1, 2) = null
7251    * ArrayUtils.removeElements([], 1, 2) = []
7252    * ArrayUtils.removeElements([1], 2, 3) = [1]
7253    * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
7254    * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
7255    * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
7256    * </pre>
7257    *
7258    * @param array the array to remove the element from, may be {@code null}
7259    * @param values the elements to be removed
7260    * @return A new array containing the existing elements except the
7261    * earliest-encountered occurrences of the specified elements.
7262    * @since 3.0.1
7263    */
 
7264  13 toggle public static double[] removeElements(final double[] array, final double... values) {
7265  13 if (isEmpty(array) || isEmpty(values)) {
7266  4 return clone(array);
7267    }
7268  9 final HashMap<Double, MutableInt> occurrences = new HashMap<>(values.length);
7269  9 for (final double v : values) {
7270  17 final Double boxed = Double.valueOf(v);
7271  17 final MutableInt count = occurrences.get(boxed);
7272  17 if (count == null) {
7273  12 occurrences.put(boxed, new MutableInt(1));
7274    } else {
7275  5 count.increment();
7276    }
7277    }
7278  9 final BitSet toRemove = new BitSet();
7279  29 for (int i = 0; i < array.length; i++) {
7280  20 final double key = array[i];
7281  20 final MutableInt count = occurrences.get(key);
7282  20 if (count != null) {
7283  13 if (count.decrementAndGet() == 0) {
7284  9 occurrences.remove(key);
7285    }
7286  13 toRemove.set(i);
7287    }
7288    }
7289  9 return (double[]) removeAll(array, toRemove);
7290    }
7291   
7292    /**
7293    * <p>Removes the elements at the specified positions from the specified array.
7294    * All remaining elements are shifted to the left.
7295    *
7296    * <p>This method returns a new array with the same elements of the input
7297    * array except those at the specified positions. The component
7298    * type of the returned array is always the same as that of the input
7299    * array.
7300    *
7301    * <p>If the input array is {@code null}, an IndexOutOfBoundsException
7302    * will be thrown, because in that case no valid index can be specified.
7303    *
7304    * <pre>
7305    * ArrayUtils.removeAll([true, false, true], 0, 2) = [false]
7306    * ArrayUtils.removeAll([true, false, true], 1, 2) = [true]
7307    * </pre>
7308    *
7309    * @param array the array to remove the element from, may not be {@code null}
7310    * @param indices the positions of the elements to be removed
7311    * @return A new array containing the existing elements except those
7312    * at the specified positions.
7313    * @throws IndexOutOfBoundsException if any index is out of range
7314    * (index &lt; 0 || index &gt;= array.length), or if the array is {@code null}.
7315    * @since 3.0.1
7316    */
 
7317  23 toggle public static boolean[] removeAll(final boolean[] array, final int... indices) {
7318  23 return (boolean[]) removeAll((Object) array, indices);
7319    }
7320   
7321    /**
7322    * <p>Removes occurrences of specified elements, in specified quantities,
7323    * from the specified array. All subsequent elements are shifted left.
7324    * For any element-to-be-removed specified in greater quantities than
7325    * contained in the original array, no change occurs beyond the
7326    * removal of the existing matching items.
7327    *
7328    * <p>This method returns a new array with the same elements of the input
7329    * array except for the earliest-encountered occurrences of the specified
7330    * elements. The component type of the returned array is always the same
7331    * as that of the input array.
7332    *
7333    * <pre>
7334    * ArrayUtils.removeElements(null, true, false) = null
7335    * ArrayUtils.removeElements([], true, false) = []
7336    * ArrayUtils.removeElements([true], false, false) = [true]
7337    * ArrayUtils.removeElements([true, false], true, true) = [false]
7338    * ArrayUtils.removeElements([true, false, true], true) = [false, true]
7339    * ArrayUtils.removeElements([true, false, true], true, true) = [false]
7340    * </pre>
7341    *
7342    * @param array the array to remove the element from, may be {@code null}
7343    * @param values the elements to be removed
7344    * @return A new array containing the existing elements except the
7345    * earliest-encountered occurrences of the specified elements.
7346    * @since 3.0.1
7347    */
 
7348  13 toggle public static boolean[] removeElements(final boolean[] array, final boolean... values) {
7349  13 if (isEmpty(array) || isEmpty(values)) {
7350  4 return clone(array);
7351    }
7352  9 final HashMap<Boolean, MutableInt> occurrences = new HashMap<>(2); // only two possible values here
7353  9 for (final boolean v : values) {
7354  17 final Boolean boxed = Boolean.valueOf(v);
7355  17 final MutableInt count = occurrences.get(boxed);
7356  17 if (count == null) {
7357  12 occurrences.put(boxed, new MutableInt(1));
7358    } else {
7359  5 count.increment();
7360    }
7361    }
7362  9 final BitSet toRemove = new BitSet();
7363  29 for (int i = 0; i < array.length; i++) {
7364  20 final boolean key = array[i];
7365  20 final MutableInt count = occurrences.get(key);
7366  20 if (count != null) {
7367  13 if (count.decrementAndGet() == 0) {
7368  9 occurrences.remove(key);
7369    }
7370  13 toRemove.set(i);
7371    }
7372    }
7373  9 return (boolean[]) removeAll(array, toRemove);
7374    }
7375   
7376    /**
7377    * Removes multiple array elements specified by index.
7378    * @param array source
7379    * @param indices to remove
7380    * @return new array of same type minus elements specified by unique values of {@code indices}
7381    * @since 3.0.1
7382    */
7383    // package protected for access by unit tests
 
7384  90181 toggle static Object removeAll(final Object array, final int... indices) {
7385  90181 final int length = getLength(array);
7386  90181 int diff = 0; // number of distinct indexes, i.e. number of entries that will be removed
7387  90181 final int[] clonedIndices = clone(indices);
7388  90181 Arrays.sort(clonedIndices);
7389   
7390    // identify length of result array
7391  90181 if (isNotEmpty(clonedIndices)) {
7392  90170 int i = clonedIndices.length;
7393  90170 int prevIndex = length;
7394  17900475 while (--i >= 0) {
7395  17810331 final int index = clonedIndices[i];
7396  17810331 if (index < 0 || index >= length) {
7397  26 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
7398    }
7399  17810305 if (index >= prevIndex) {
7400  7 continue;
7401    }
7402  17810298 diff++;
7403  17810298 prevIndex = index;
7404    }
7405    }
7406   
7407    // create result array
7408  90155 final Object result = Array.newInstance(array.getClass().getComponentType(), length - diff);
7409  90155 if (diff < length) {
7410  70115 int end = length; // index just after last copy
7411  70115 int dest = length - diff; // number of entries so far not copied
7412  6880352 for (int i = clonedIndices.length - 1; i >= 0; i--) {
7413  6810237 final int index = clonedIndices[i];
7414  6810237 if (end - index > 1) { // same as (cp > 0)
7415  70138 final int cp = end - index - 1;
7416  70138 dest -= cp;
7417  70138 System.arraycopy(array, index + 1, result, dest, cp);
7418    // Afer this copy, we still have room for dest items.
7419    }
7420  6810237 end = index;
7421    }
7422  70115 if (end > 0) {
7423  10065 System.arraycopy(array, 0, result, 0, end);
7424    }
7425    }
7426  90155 return result;
7427    }
7428   
7429    /**
7430    * Removes multiple array elements specified by indices.
7431    *
7432    * @param array source
7433    * @param indices to remove
7434    * @return new array of same type minus elements specified by the set bits in {@code indices}
7435    * @since 3.2
7436    */
7437    // package protected for access by unit tests
 
7438  90081 toggle static Object removeAll(final Object array, final BitSet indices) {
7439  90081 final int srcLength = ArrayUtils.getLength(array);
7440    // No need to check maxIndex here, because method only currently called from removeElements()
7441    // which guarantee to generate on;y valid bit entries.
7442    // final int maxIndex = indices.length();
7443    // if (maxIndex > srcLength) {
7444    // throw new IndexOutOfBoundsException("Index: " + (maxIndex-1) + ", Length: " + srcLength);
7445    // }
7446  90081 final int removals = indices.cardinality(); // true bits are items to remove
7447  90081 final Object result = Array.newInstance(array.getClass().getComponentType(), srcLength - removals);
7448  90081 int srcIndex = 0;
7449  90081 int destIndex = 0;
7450  90081 int count;
7451  90081 int set;
7452  ? while ((set = indices.nextSetBit(srcIndex)) != -1) {
7453  90099 count = set - srcIndex;
7454  90099 if (count > 0) {
7455  10018 System.arraycopy(array, srcIndex, result, destIndex, count);
7456  10018 destIndex += count;
7457    }
7458  90099 srcIndex = indices.nextClearBit(set);
7459    }
7460  90081 count = srcLength - srcIndex;
7461  90081 if (count > 0) {
7462  70037 System.arraycopy(array, srcIndex, result, destIndex, count);
7463    }
7464  90081 return result;
7465    }
7466   
7467    /**
7468    * <p>This method checks whether the provided array is sorted according to the class's
7469    * {@code compareTo} method.
7470    *
7471    * @param array the array to check
7472    * @param <T> the datatype of the array to check, it must implement {@code Comparable}
7473    * @return whether the array is sorted
7474    * @since 3.4
7475    */
 
7476  4 toggle public static <T extends Comparable<? super T>> boolean isSorted(final T[] array) {
7477  4 return isSorted(array, new Comparator<T>() {
 
7478  4 toggle @Override
7479    public int compare(final T o1, final T o2) {
7480  4 return o1.compareTo(o2);
7481    }
7482    });
7483    }
7484   
7485   
7486    /**
7487    * <p>This method checks whether the provided array is sorted according to the provided {@code Comparator}.
7488    *
7489    * @param array the array to check
7490    * @param comparator the {@code Comparator} to compare over
7491    * @param <T> the datatype of the array
7492    * @return whether the array is sorted
7493    * @since 3.4
7494    */
 
7495  9 toggle public static <T> boolean isSorted(final T[] array, final Comparator<T> comparator) {
7496  9 if (comparator == null) {
7497  1 throw new IllegalArgumentException("Comparator should not be null.");
7498    }
7499   
7500  8 if (array == null || array.length < 2) {
7501  4 return true;
7502    }
7503   
7504  4 T previous = array[0];
7505  4 final int n = array.length;
7506  9 for (int i = 1; i < n; i++) {
7507  7 final T current = array[i];
7508  7 if (comparator.compare(previous, current) > 0) {
7509  2 return false;
7510    }
7511   
7512  5 previous = current;
7513    }
7514  2 return true;
7515    }
7516   
7517    /**
7518    * <p>This method checks whether the provided array is sorted according to natural ordering.
7519    *
7520    * @param array the array to check
7521    * @return whether the array is sorted according to natural ordering
7522    * @since 3.4
7523    */
 
7524  4 toggle public static boolean isSorted(final int[] array) {
7525  4 if (array == null || array.length < 2) {
7526  2 return true;
7527    }
7528   
7529  2 int previous = array[0];
7530  2 final int n = array.length;
7531  5 for (int i = 1; i < n; i++) {
7532  4 final int current = array[i];
7533  4 if (NumberUtils.compare(previous, current) > 0) {
7534  1 return false;
7535    }
7536   
7537  3 previous = current;
7538    }
7539  1 return true;
7540    }
7541   
7542    /**
7543    * <p>This method checks whether the provided array is sorted according to natural ordering.
7544    *
7545    * @param array the array to check
7546    * @return whether the array is sorted according to natural ordering
7547    * @since 3.4
7548    */
 
7549  4 toggle public static boolean isSorted(final long[] array) {
7550  4 if (array == null || array.length < 2) {
7551  2 return true;
7552    }
7553   
7554  2 long previous = array[0];
7555  2 final int n = array.length;
7556  5 for (int i = 1; i < n; i++) {
7557  4 final long current = array[i];
7558  4 if (NumberUtils.compare(previous, current) > 0) {
7559  1 return false;
7560    }
7561   
7562  3 previous = current;
7563    }
7564  1 return true;
7565    }
7566   
7567    /**
7568    * <p>This method checks whether the provided array is sorted according to natural ordering.
7569    *
7570    * @param array the array to check
7571    * @return whether the array is sorted according to natural ordering
7572    * @since 3.4
7573    */
 
7574  4 toggle public static boolean isSorted(final short[] array) {
7575  4 if (array == null || array.length < 2) {
7576  2 return true;
7577    }
7578   
7579  2 short previous = array[0];
7580  2 final int n = array.length;
7581  5 for (int i = 1; i < n; i++) {
7582  4 final short current = array[i];
7583  4 if (NumberUtils.compare(previous, current) > 0) {
7584  1 return false;
7585    }
7586   
7587  3 previous = current;
7588    }
7589  1 return true;
7590    }
7591   
7592    /**
7593    * <p>This method checks whether the provided array is sorted according to natural ordering.
7594    *
7595    * @param array the array to check
7596    * @return whether the array is sorted according to natural ordering
7597    * @since 3.4
7598    */
 
7599  4 toggle public static boolean isSorted(final double[] array) {
7600  4 if (array == null || array.length < 2) {
7601  2 return true;
7602    }
7603   
7604  2 double previous = array[0];
7605  2 final int n = array.length;
7606  6 for (int i = 1; i < n; i++) {
7607  5 final double current = array[i];
7608  5 if (Double.compare(previous, current) > 0) {
7609  1 return false;
7610    }
7611   
7612  4 previous = current;
7613    }
7614  1 return true;
7615    }
7616   
7617    /**
7618    * <p>This method checks whether the provided array is sorted according to natural ordering.
7619    *
7620    * @param array the array to check
7621    * @return whether the array is sorted according to natural ordering
7622    * @since 3.4
7623    */
 
7624  4 toggle public static boolean isSorted(final float[] array) {
7625  4 if (array == null || array.length < 2) {
7626  2 return true;
7627    }
7628   
7629  2 float previous = array[0];
7630  2 final int n = array.length;
7631  6 for (int i = 1; i < n; i++) {
7632  5 final float current = array[i];
7633  5 if (Float.compare(previous, current) > 0) {
7634  1 return false;
7635    }
7636   
7637  4 previous = current;
7638    }
7639  1 return true;
7640    }
7641   
7642    /**
7643    * <p>This method checks whether the provided array is sorted according to natural ordering.
7644    *
7645    * @param array the array to check
7646    * @return whether the array is sorted according to natural ordering
7647    * @since 3.4
7648    */
 
7649  4 toggle public static boolean isSorted(final byte[] array) {
7650  4 if (array == null || array.length < 2) {
7651  2 return true;
7652    }
7653   
7654  2 byte previous = array[0];
7655  2 final int n = array.length;
7656  5 for (int i = 1; i < n; i++) {
7657  4 final byte current = array[i];
7658  4 if (NumberUtils.compare(previous, current) > 0) {
7659  1 return false;
7660    }
7661   
7662  3 previous = current;
7663    }
7664  1 return true;
7665    }
7666   
7667    /**
7668    * <p>This method checks whether the provided array is sorted according to natural ordering.
7669    *
7670    * @param array the array to check
7671    * @return whether the array is sorted according to natural ordering
7672    * @since 3.4
7673    */
 
7674  4 toggle public static boolean isSorted(final char[] array) {
7675  4 if (array == null || array.length < 2) {
7676  2 return true;
7677    }
7678   
7679  2 char previous = array[0];
7680  2 final int n = array.length;
7681  5 for (int i = 1; i < n; i++) {
7682  4 final char current = array[i];
7683  4 if (CharUtils.compare(previous, current) > 0) {
7684  1 return false;
7685    }
7686   
7687  3 previous = current;
7688    }
7689  1 return true;
7690    }
7691   
7692    /**
7693    * <p>This method checks whether the provided array is sorted according to natural ordering
7694    * ({@code false} before {@code true}).
7695    *
7696    * @param array the array to check
7697    * @return whether the array is sorted according to natural ordering
7698    * @since 3.4
7699    */
 
7700  4 toggle public static boolean isSorted(final boolean[] array) {
7701  4 if (array == null || array.length < 2) {
7702  2 return true;
7703    }
7704   
7705  2 boolean previous = array[0];
7706  2 final int n = array.length;
7707  3 for (int i = 1; i < n; i++) {
7708  2 final boolean current = array[i];
7709  2 if (BooleanUtils.compare(previous, current) > 0) {
7710  1 return false;
7711    }
7712   
7713  1 previous = current;
7714    }
7715  1 return true;
7716    }
7717   
7718    /**
7719    * Removes the occurrences of the specified element from the specified boolean array.
7720    *
7721    * <p>
7722    * All subsequent elements are shifted to the left (subtracts one from their indices).
7723    * If the array doesn't contains such an element, no elements are removed from the array.
7724    * <code>null</code> will be returned if the input array is <code>null</code>.
7725    * </p>
7726    *
7727    * @param element the element to remove
7728    * @param array the input array
7729    *
7730    * @return A new array containing the existing elements except the occurrences of the specified element.
7731    * @since 3.5
7732    */
 
7733  7 toggle public static boolean[] removeAllOccurences(final boolean[] array, final boolean element) {
7734  7 int index = indexOf(array, element);
7735  7 if (index == INDEX_NOT_FOUND) {
7736  2 return clone(array);
7737    }
7738   
7739  5 final int[] indices = new int[array.length - index];
7740  5 indices[0] = index;
7741  5 int count = 1;
7742   
7743  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7744  8 indices[count++] = index;
7745    }
7746   
7747  5 return removeAll(array, Arrays.copyOf(indices, count));
7748    }
7749   
7750    /**
7751    * Removes the occurrences of the specified element from the specified char array.
7752    *
7753    * <p>
7754    * All subsequent elements are shifted to the left (subtracts one from their indices).
7755    * If the array doesn't contains such an element, no elements are removed from the array.
7756    * <code>null</code> will be returned if the input array is <code>null</code>.
7757    * </p>
7758    *
7759    * @param element the element to remove
7760    * @param array the input array
7761    *
7762    * @return A new array containing the existing elements except the occurrences of the specified element.
7763    * @since 3.5
7764    */
 
7765  6 toggle public static char[] removeAllOccurences(final char[] array, final char element) {
7766  6 int index = indexOf(array, element);
7767  6 if (index == INDEX_NOT_FOUND) {
7768  3 return clone(array);
7769    }
7770   
7771  3 final int[] indices = new int[array.length - index];
7772  3 indices[0] = index;
7773  3 int count = 1;
7774   
7775  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7776  3 indices[count++] = index;
7777    }
7778   
7779  3 return removeAll(array, Arrays.copyOf(indices, count));
7780    }
7781   
7782    /**
7783    * Removes the occurrences of the specified element from the specified byte array.
7784    *
7785    * <p>
7786    * All subsequent elements are shifted to the left (subtracts one from their indices).
7787    * If the array doesn't contains such an element, no elements are removed from the array.
7788    * <code>null</code> will be returned if the input array is <code>null</code>.
7789    * </p>
7790    *
7791    * @param element the element to remove
7792    * @param array the input array
7793    *
7794    * @return A new array containing the existing elements except the occurrences of the specified element.
7795    * @since 3.5
7796    */
 
7797  6 toggle public static byte[] removeAllOccurences(final byte[] array, final byte element) {
7798  6 int index = indexOf(array, element);
7799  6 if (index == INDEX_NOT_FOUND) {
7800  3 return clone(array);
7801    }
7802   
7803  3 final int[] indices = new int[array.length - index];
7804  3 indices[0] = index;
7805  3 int count = 1;
7806   
7807  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7808  3 indices[count++] = index;
7809    }
7810   
7811  3 return removeAll(array, Arrays.copyOf(indices, count));
7812    }
7813   
7814    /**
7815    * Removes the occurrences of the specified element from the specified short array.
7816    *
7817    * <p>
7818    * All subsequent elements are shifted to the left (subtracts one from their indices).
7819    * If the array doesn't contains such an element, no elements are removed from the array.
7820    * <code>null</code> will be returned if the input array is <code>null</code>.
7821    * </p>
7822    *
7823    * @param element the element to remove
7824    * @param array the input array
7825    *
7826    * @return A new array containing the existing elements except the occurrences of the specified element.
7827    * @since 3.5
7828    */
 
7829  6 toggle public static short[] removeAllOccurences(final short[] array, final short element) {
7830  6 int index = indexOf(array, element);
7831  6 if (index == INDEX_NOT_FOUND) {
7832  3 return clone(array);
7833    }
7834   
7835  3 final int[] indices = new int[array.length - index];
7836  3 indices[0] = index;
7837  3 int count = 1;
7838   
7839  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7840  3 indices[count++] = index;
7841    }
7842   
7843  3 return removeAll(array, Arrays.copyOf(indices, count));
7844    }
7845   
7846    /**
7847    * Removes the occurrences of the specified element from the specified int array.
7848    *
7849    * <p>
7850    * All subsequent elements are shifted to the left (subtracts one from their indices).
7851    * If the array doesn't contains such an element, no elements are removed from the array.
7852    * <code>null</code> will be returned if the input array is <code>null</code>.
7853    * </p>
7854    *
7855    * @param element the element to remove
7856    * @param array the input array
7857    *
7858    * @return A new array containing the existing elements except the occurrences of the specified element.
7859    * @since 3.5
7860    */
 
7861  6 toggle public static int[] removeAllOccurences(final int[] array, final int element) {
7862  6 int index = indexOf(array, element);
7863  6 if (index == INDEX_NOT_FOUND) {
7864  3 return clone(array);
7865    }
7866   
7867  3 final int[] indices = new int[array.length - index];
7868  3 indices[0] = index;
7869  3 int count = 1;
7870   
7871  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7872  3 indices[count++] = index;
7873    }
7874   
7875  3 return removeAll(array, Arrays.copyOf(indices, count));
7876    }
7877   
7878    /**
7879    * Removes the occurrences of the specified element from the specified long array.
7880    *
7881    * <p>
7882    * All subsequent elements are shifted to the left (subtracts one from their indices).
7883    * If the array doesn't contains such an element, no elements are removed from the array.
7884    * <code>null</code> will be returned if the input array is <code>null</code>.
7885    * </p>
7886    *
7887    * @param element the element to remove
7888    * @param array the input array
7889    *
7890    * @return A new array containing the existing elements except the occurrences of the specified element.
7891    * @since 3.5
7892    */
 
7893  6 toggle public static long[] removeAllOccurences(final long[] array, final long element) {
7894  6 int index = indexOf(array, element);
7895  6 if (index == INDEX_NOT_FOUND) {
7896  3 return clone(array);
7897    }
7898   
7899  3 final int[] indices = new int[array.length - index];
7900  3 indices[0] = index;
7901  3 int count = 1;
7902   
7903  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7904  3 indices[count++] = index;
7905    }
7906   
7907  3 return removeAll(array, Arrays.copyOf(indices, count));
7908    }
7909   
7910    /**
7911    * Removes the occurrences of the specified element from the specified float array.
7912    *
7913    * <p>
7914    * All subsequent elements are shifted to the left (subtracts one from their indices).
7915    * If the array doesn't contains such an element, no elements are removed from the array.
7916    * <code>null</code> will be returned if the input array is <code>null</code>.
7917    * </p>
7918    *
7919    * @param element the element to remove
7920    * @param array the input array
7921    *
7922    * @return A new array containing the existing elements except the occurrences of the specified element.
7923    * @since 3.5
7924    */
 
7925  6 toggle public static float[] removeAllOccurences(final float[] array, final float element) {
7926  6 int index = indexOf(array, element);
7927  6 if (index == INDEX_NOT_FOUND) {
7928  3 return clone(array);
7929    }
7930   
7931  3 final int[] indices = new int[array.length - index];
7932  3 indices[0] = index;
7933  3 int count = 1;
7934   
7935  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7936  3 indices[count++] = index;
7937    }
7938   
7939  3 return removeAll(array, Arrays.copyOf(indices, count));
7940    }
7941   
7942    /**
7943    * Removes the occurrences of the specified element from the specified double array.
7944    *
7945    * <p>
7946    * All subsequent elements are shifted to the left (subtracts one from their indices).
7947    * If the array doesn't contains such an element, no elements are removed from the array.
7948    * <code>null</code> will be returned if the input array is <code>null</code>.
7949    * </p>
7950    *
7951    * @param element the element to remove
7952    * @param array the input array
7953    *
7954    * @return A new array containing the existing elements except the occurrences of the specified element.
7955    * @since 3.5
7956    */
 
7957  6 toggle public static double[] removeAllOccurences(final double[] array, final double element) {
7958  6 int index = indexOf(array, element);
7959  6 if (index == INDEX_NOT_FOUND) {
7960  3 return clone(array);
7961    }
7962   
7963  3 final int[] indices = new int[array.length - index];
7964  3 indices[0] = index;
7965  3 int count = 1;
7966   
7967  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
7968  3 indices[count++] = index;
7969    }
7970   
7971  3 return removeAll(array, Arrays.copyOf(indices, count));
7972    }
7973   
7974    /**
7975    * Removes the occurrences of the specified element from the specified array.
7976    *
7977    * <p>
7978    * All subsequent elements are shifted to the left (subtracts one from their indices).
7979    * If the array doesn't contains such an element, no elements are removed from the array.
7980    * <code>null</code> will be returned if the input array is <code>null</code>.
7981    * </p>
7982    *
7983    * @param <T> the type of object in the array
7984    * @param element the element to remove
7985    * @param array the input array
7986    *
7987    * @return A new array containing the existing elements except the occurrences of the specified element.
7988    * @since 3.5
7989    */
 
7990  6 toggle public static <T> T[] removeAllOccurences(final T[] array, final T element) {
7991  6 int index = indexOf(array, element);
7992  6 if (index == INDEX_NOT_FOUND) {
7993  3 return clone(array);
7994    }
7995   
7996  3 final int[] indices = new int[array.length - index];
7997  3 indices[0] = index;
7998  3 int count = 1;
7999   
8000  ? while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND) {
8001  3 indices[count++] = index;
8002    }
8003   
8004  3 return removeAll(array, Arrays.copyOf(indices, count));
8005    }
8006   
8007    /**
8008    * <p>Returns an array containing the string representation of each element in the argument array.</p>
8009    *
8010    * <p>This method returns {@code null} for a {@code null} input array.</p>
8011    *
8012    * @param array the {@code Object[]} to be processed, may be null
8013    * @return {@code String[]} of the same size as the source with its element's string representation,
8014    * {@code null} if null array input
8015    * @throws NullPointerException if array contains {@code null}
8016    * @since 3.6
8017    */
 
8018  4 toggle public static String[] toStringArray(final Object[] array) {
8019  4 if (array == null) {
8020  1 return null;
8021  3 } else if (array.length == 0) {
8022  1 return EMPTY_STRING_ARRAY;
8023    }
8024   
8025  2 final String[] result = new String[array.length];
8026  7 for (int i = 0; i < array.length; i++) {
8027  6 result[i] = array[i].toString();
8028    }
8029   
8030  1 return result;
8031    }
8032   
8033    /**
8034    * <p>Returns an array containing the string representation of each element in the argument
8035    * array handling {@code null} elements.</p>
8036    *
8037    * <p>This method returns {@code null} for a {@code null} input array.</p>
8038    *
8039    * @param array the Object[] to be processed, may be null
8040    * @param valueForNullElements the value to insert if {@code null} is found
8041    * @return a {@code String} array, {@code null} if null array input
8042    * @since 3.6
8043    */
 
8044  3 toggle public static String[] toStringArray(final Object[] array, final String valueForNullElements) {
8045  3 if (null == array) {
8046  1 return null;
8047  2 } else if (array.length == 0) {
8048  1 return EMPTY_STRING_ARRAY;
8049    }
8050   
8051  1 final String[] result = new String[array.length];
8052  4 for (int i = 0; i < array.length; i++) {
8053  3 final Object object = array[i];
8054  3 result[i] = (object == null ? valueForNullElements : object.toString());
8055    }
8056   
8057  1 return result;
8058    }
8059   
8060    /**
8061    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8062    *
8063    * <p>When an array is returned, it is always a new array.</p>
8064    *
8065    * <pre>
8066    * ArrayUtils.insert(index, null, null) = null
8067    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8068    * ArrayUtils.insert(index, null, values) = null
8069    * </pre>
8070    *
8071    * @param index the position within {@code array} to insert the new values
8072    * @param array the array to insert the values into, may be {@code null}
8073    * @param values the new values to insert, may be {@code null}
8074    * @return The new array.
8075    * @throws IndexOutOfBoundsException if {@code array} is provided
8076    * and either {@code index < 0} or {@code index > array.length}
8077    * @since 3.6
8078    */
 
8079  12 toggle public static boolean[] insert(final int index, final boolean[] array, final boolean... values) {
8080  12 if (array == null) {
8081  2 return null;
8082    }
8083  10 if (values == null || values.length == 0) {
8084  2 return clone(array);
8085    }
8086  8 if (index < 0 || index > array.length) {
8087  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8088    }
8089   
8090  6 boolean[] result = new boolean[array.length + values.length];
8091   
8092  6 System.arraycopy(values, 0, result, index, values.length);
8093  6 if (index > 0) {
8094  4 System.arraycopy(array, 0, result, 0, index);
8095    }
8096  6 if (index < array.length) {
8097  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8098    }
8099  6 return result;
8100    }
8101   
8102    /**
8103    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8104    *
8105    * <p>When an array is returned, it is always a new array.</p>
8106    *
8107    * <pre>
8108    * ArrayUtils.insert(index, null, null) = null
8109    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8110    * ArrayUtils.insert(index, null, values) = null
8111    * </pre>
8112    *
8113    * @param index the position within {@code array} to insert the new values
8114    * @param array the array to insert the values into, may be {@code null}
8115    * @param values the new values to insert, may be {@code null}
8116    * @return The new array.
8117    * @throws IndexOutOfBoundsException if {@code array} is provided
8118    * and either {@code index < 0} or {@code index > array.length}
8119    * @since 3.6
8120    */
 
8121  12 toggle public static byte[] insert(final int index, final byte[] array, final byte... values) {
8122  12 if (array == null) {
8123  2 return null;
8124    }
8125  10 if (values == null || values.length == 0) {
8126  2 return clone(array);
8127    }
8128  8 if (index < 0 || index > array.length) {
8129  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8130    }
8131   
8132  6 byte[] result = new byte[array.length + values.length];
8133   
8134  6 System.arraycopy(values, 0, result, index, values.length);
8135  6 if (index > 0) {
8136  4 System.arraycopy(array, 0, result, 0, index);
8137    }
8138  6 if (index < array.length) {
8139  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8140    }
8141  6 return result;
8142    }
8143   
8144    /**
8145    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8146    *
8147    * <p>When an array is returned, it is always a new array.</p>
8148    *
8149    * <pre>
8150    * ArrayUtils.insert(index, null, null) = null
8151    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8152    * ArrayUtils.insert(index, null, values) = null
8153    * </pre>
8154    *
8155    * @param index the position within {@code array} to insert the new values
8156    * @param array the array to insert the values into, may be {@code null}
8157    * @param values the new values to insert, may be {@code null}
8158    * @return The new array.
8159    * @throws IndexOutOfBoundsException if {@code array} is provided
8160    * and either {@code index < 0} or {@code index > array.length}
8161    * @since 3.6
8162    */
 
8163  12 toggle public static char[] insert(final int index, final char[] array, final char... values) {
8164  12 if (array == null) {
8165  2 return null;
8166    }
8167  10 if (values == null || values.length == 0) {
8168  2 return clone(array);
8169    }
8170  8 if (index < 0 || index > array.length) {
8171  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8172    }
8173   
8174  6 char[] result = new char[array.length + values.length];
8175   
8176  6 System.arraycopy(values, 0, result, index, values.length);
8177  6 if (index > 0) {
8178  4 System.arraycopy(array, 0, result, 0, index);
8179    }
8180  6 if (index < array.length) {
8181  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8182    }
8183  6 return result;
8184    }
8185   
8186    /**
8187    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8188    *
8189    * <p>When an array is returned, it is always a new array.</p>
8190    *
8191    * <pre>
8192    * ArrayUtils.insert(index, null, null) = null
8193    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8194    * ArrayUtils.insert(index, null, values) = null
8195    * </pre>
8196    *
8197    * @param index the position within {@code array} to insert the new values
8198    * @param array the array to insert the values into, may be {@code null}
8199    * @param values the new values to insert, may be {@code null}
8200    * @return The new array.
8201    * @throws IndexOutOfBoundsException if {@code array} is provided
8202    * and either {@code index < 0} or {@code index > array.length}
8203    * @since 3.6
8204    */
 
8205  12 toggle public static double[] insert(final int index, final double[] array, final double... values) {
8206  12 if (array == null) {
8207  2 return null;
8208    }
8209  10 if (values == null || values.length == 0) {
8210  2 return clone(array);
8211    }
8212  8 if (index < 0 || index > array.length) {
8213  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8214    }
8215   
8216  6 double[] result = new double[array.length + values.length];
8217   
8218  6 System.arraycopy(values, 0, result, index, values.length);
8219  6 if (index > 0) {
8220  4 System.arraycopy(array, 0, result, 0, index);
8221    }
8222  6 if (index < array.length) {
8223  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8224    }
8225  6 return result;
8226    }
8227   
8228    /**
8229    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8230    *
8231    * <p>When an array is returned, it is always a new array.</p>
8232    *
8233    * <pre>
8234    * ArrayUtils.insert(index, null, null) = null
8235    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8236    * ArrayUtils.insert(index, null, values) = null
8237    * </pre>
8238    *
8239    * @param index the position within {@code array} to insert the new values
8240    * @param array the array to insert the values into, may be {@code null}
8241    * @param values the new values to insert, may be {@code null}
8242    * @return The new array.
8243    * @throws IndexOutOfBoundsException if {@code array} is provided
8244    * and either {@code index < 0} or {@code index > array.length}
8245    * @since 3.6
8246    */
 
8247  12 toggle public static float[] insert(final int index, final float[] array, final float... values) {
8248  12 if (array == null) {
8249  2 return null;
8250    }
8251  10 if (values == null || values.length == 0) {
8252  2 return clone(array);
8253    }
8254  8 if (index < 0 || index > array.length) {
8255  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8256    }
8257   
8258  6 float[] result = new float[array.length + values.length];
8259   
8260  6 System.arraycopy(values, 0, result, index, values.length);
8261  6 if (index > 0) {
8262  4 System.arraycopy(array, 0, result, 0, index);
8263    }
8264  6 if (index < array.length) {
8265  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8266    }
8267  6 return result;
8268    }
8269   
8270    /**
8271    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8272    *
8273    * <p>When an array is returned, it is always a new array.</p>
8274    *
8275    * <pre>
8276    * ArrayUtils.insert(index, null, null) = null
8277    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8278    * ArrayUtils.insert(index, null, values) = null
8279    * </pre>
8280    *
8281    * @param index the position within {@code array} to insert the new values
8282    * @param array the array to insert the values into, may be {@code null}
8283    * @param values the new values to insert, may be {@code null}
8284    * @return The new array.
8285    * @throws IndexOutOfBoundsException if {@code array} is provided
8286    * and either {@code index < 0} or {@code index > array.length}
8287    * @since 3.6
8288    */
 
8289  12 toggle public static int[] insert(final int index, final int[] array, final int... values) {
8290  12 if (array == null) {
8291  2 return null;
8292    }
8293  10 if (values == null || values.length == 0) {
8294  2 return clone(array);
8295    }
8296  8 if (index < 0 || index > array.length) {
8297  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8298    }
8299   
8300  6 int[] result = new int[array.length + values.length];
8301   
8302  6 System.arraycopy(values, 0, result, index, values.length);
8303  6 if (index > 0) {
8304  4 System.arraycopy(array, 0, result, 0, index);
8305    }
8306  6 if (index < array.length) {
8307  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8308    }
8309  6 return result;
8310    }
8311   
8312    /**
8313    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8314    *
8315    * <p>When an array is returned, it is always a new array.</p>
8316    *
8317    * <pre>
8318    * ArrayUtils.insert(index, null, null) = null
8319    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8320    * ArrayUtils.insert(index, null, values) = null
8321    * </pre>
8322    *
8323    * @param index the position within {@code array} to insert the new values
8324    * @param array the array to insert the values into, may be {@code null}
8325    * @param values the new values to insert, may be {@code null}
8326    * @return The new array.
8327    * @throws IndexOutOfBoundsException if {@code array} is provided
8328    * and either {@code index < 0} or {@code index > array.length}
8329    * @since 3.6
8330    */
 
8331  12 toggle public static long[] insert(final int index, final long[] array, final long... values) {
8332  12 if (array == null) {
8333  2 return null;
8334    }
8335  10 if (values == null || values.length == 0) {
8336  2 return clone(array);
8337    }
8338  8 if (index < 0 || index > array.length) {
8339  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8340    }
8341   
8342  6 long[] result = new long[array.length + values.length];
8343   
8344  6 System.arraycopy(values, 0, result, index, values.length);
8345  6 if (index > 0) {
8346  4 System.arraycopy(array, 0, result, 0, index);
8347    }
8348  6 if (index < array.length) {
8349  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8350    }
8351  6 return result;
8352    }
8353   
8354    /**
8355    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8356    *
8357    * <p>When an array is returned, it is always a new array.</p>
8358    *
8359    * <pre>
8360    * ArrayUtils.insert(index, null, null) = null
8361    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8362    * ArrayUtils.insert(index, null, values) = null
8363    * </pre>
8364    *
8365    * @param index the position within {@code array} to insert the new values
8366    * @param array the array to insert the values into, may be {@code null}
8367    * @param values the new values to insert, may be {@code null}
8368    * @return The new array.
8369    * @throws IndexOutOfBoundsException if {@code array} is provided
8370    * and either {@code index < 0} or {@code index > array.length}
8371    * @since 3.6
8372    */
 
8373  12 toggle public static short[] insert(final int index, final short[] array, final short... values) {
8374  12 if (array == null) {
8375  2 return null;
8376    }
8377  10 if (values == null || values.length == 0) {
8378  2 return clone(array);
8379    }
8380  8 if (index < 0 || index > array.length) {
8381  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8382    }
8383   
8384  6 short[] result = new short[array.length + values.length];
8385   
8386  6 System.arraycopy(values, 0, result, index, values.length);
8387  6 if (index > 0) {
8388  4 System.arraycopy(array, 0, result, 0, index);
8389    }
8390  6 if (index < array.length) {
8391  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8392    }
8393  6 return result;
8394    }
8395   
8396    /**
8397    * <p>Inserts elements into an array at the given index (starting from zero).</p>
8398    *
8399    * <p>When an array is returned, it is always a new array.</p>
8400    *
8401    * <pre>
8402    * ArrayUtils.insert(index, null, null) = null
8403    * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
8404    * ArrayUtils.insert(index, null, values) = null
8405    * </pre>
8406    *
8407    * @param <T> The type of elements in {@code array} and {@code values}
8408    * @param index the position within {@code array} to insert the new values
8409    * @param array the array to insert the values into, may be {@code null}
8410    * @param values the new values to insert, may be {@code null}
8411    * @return The new array.
8412    * @throws IndexOutOfBoundsException if {@code array} is provided
8413    * and either {@code index < 0} or {@code index > array.length}
8414    * @since 3.6
8415    */
 
8416  12 toggle @SafeVarargs
8417    public static <T> T[] insert(final int index, final T[] array, final T... values) {
8418    /*
8419    * Note on use of @SafeVarargs:
8420    *
8421    * By returning null when 'array' is null, we avoid returning the vararg
8422    * array to the caller. We also avoid relying on the type of the vararg
8423    * array, by inspecting the component type of 'array'.
8424    */
8425   
8426  12 if (array == null) {
8427  2 return null;
8428    }
8429  10 if (values == null || values.length == 0) {
8430  2 return clone(array);
8431    }
8432  8 if (index < 0 || index > array.length) {
8433  2 throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
8434    }
8435   
8436  6 final Class<?> type = array.getClass().getComponentType();
8437  6 @SuppressWarnings("unchecked") // OK, because array and values are of type T
8438    T[] result = (T[]) Array.newInstance(type, array.length + values.length);
8439   
8440  6 System.arraycopy(values, 0, result, index, values.length);
8441  6 if (index > 0) {
8442  4 System.arraycopy(array, 0, result, 0, index);
8443    }
8444  6 if (index < array.length) {
8445  4 System.arraycopy(array, index, result, index + values.length, array.length - index);
8446    }
8447  6 return result;
8448    }
8449   
8450    /**
8451    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8452    *
8453    * @param array the array to shuffle
8454    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8455    * @since 3.6
8456    */
 
8457  1 toggle public static void shuffle(Object[] array) {
8458  1 shuffle(array, new Random());
8459    }
8460   
8461    /**
8462    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8463    *
8464    * @param array the array to shuffle
8465    * @param random the source of randomness used to permute the elements
8466    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8467    * @since 3.6
8468    */
 
8469  1 toggle public static void shuffle(Object[] array, Random random) {
8470  10 for (int i = array.length; i > 1; i--) {
8471  9 swap(array, i - 1, random.nextInt(i), 1);
8472    }
8473    }
8474   
8475    /**
8476    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8477    *
8478    * @param array the array to shuffle
8479    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8480    * @since 3.6
8481    */
 
8482  1 toggle public static void shuffle(boolean[] array) {
8483  1 shuffle(array, new Random());
8484    }
8485   
8486    /**
8487    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8488    *
8489    * @param array the array to shuffle
8490    * @param random the source of randomness used to permute the elements
8491    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8492    * @since 3.6
8493    */
 
8494  1 toggle public static void shuffle(boolean[] array, Random random) {
8495  10 for (int i = array.length; i > 1; i--) {
8496  9 swap(array, i - 1, random.nextInt(i), 1);
8497    }
8498    }
8499   
8500    /**
8501    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8502    *
8503    * @param array the array to shuffle
8504    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8505    * @since 3.6
8506    */
 
8507  1 toggle public static void shuffle(byte[] array) {
8508  1 shuffle(array, new Random());
8509    }
8510   
8511    /**
8512    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8513    *
8514    * @param array the array to shuffle
8515    * @param random the source of randomness used to permute the elements
8516    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8517    * @since 3.6
8518    */
 
8519  1 toggle public static void shuffle(byte[] array, Random random) {
8520  10 for (int i = array.length; i > 1; i--) {
8521  9 swap(array, i - 1, random.nextInt(i), 1);
8522    }
8523    }
8524   
8525    /**
8526    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8527    *
8528    * @param array the array to shuffle
8529    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8530    * @since 3.6
8531    */
 
8532  1 toggle public static void shuffle(char[] array) {
8533  1 shuffle(array, new Random());
8534    }
8535   
8536    /**
8537    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8538    *
8539    * @param array the array to shuffle
8540    * @param random the source of randomness used to permute the elements
8541    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8542    * @since 3.6
8543    */
 
8544  1 toggle public static void shuffle(char[] array, Random random) {
8545  10 for (int i = array.length; i > 1; i--) {
8546  9 swap(array, i - 1, random.nextInt(i), 1);
8547    }
8548    }
8549   
8550    /**
8551    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8552    *
8553    * @param array the array to shuffle
8554    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8555    * @since 3.6
8556    */
 
8557  1 toggle public static void shuffle(short[] array) {
8558  1 shuffle(array, new Random());
8559    }
8560   
8561    /**
8562    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8563    *
8564    * @param array the array to shuffle
8565    * @param random the source of randomness used to permute the elements
8566    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8567    * @since 3.6
8568    */
 
8569  1 toggle public static void shuffle(short[] array, Random random) {
8570  10 for (int i = array.length; i > 1; i--) {
8571  9 swap(array, i - 1, random.nextInt(i), 1);
8572    }
8573    }
8574   
8575    /**
8576    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8577    *
8578    * @param array the array to shuffle
8579    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8580    * @since 3.6
8581    */
 
8582  1 toggle public static void shuffle(int[] array) {
8583  1 shuffle(array, new Random());
8584    }
8585   
8586    /**
8587    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8588    *
8589    * @param array the array to shuffle
8590    * @param random the source of randomness used to permute the elements
8591    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8592    * @since 3.6
8593    */
 
8594  1 toggle public static void shuffle(int[] array, Random random) {
8595  10 for (int i = array.length; i > 1; i--) {
8596  9 swap(array, i - 1, random.nextInt(i), 1);
8597    }
8598    }
8599   
8600    /**
8601    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8602    *
8603    * @param array the array to shuffle
8604    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8605    * @since 3.6
8606    */
 
8607  1 toggle public static void shuffle(long[] array) {
8608  1 shuffle(array, new Random());
8609    }
8610   
8611    /**
8612    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8613    *
8614    * @param array the array to shuffle
8615    * @param random the source of randomness used to permute the elements
8616    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8617    * @since 3.6
8618    */
 
8619  1 toggle public static void shuffle(long[] array, Random random) {
8620  10 for (int i = array.length; i > 1; i--) {
8621  9 swap(array, i - 1, random.nextInt(i), 1);
8622    }
8623    }
8624   
8625    /**
8626    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8627    *
8628    * @param array the array to shuffle
8629    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8630    * @since 3.6
8631    */
 
8632  1 toggle public static void shuffle(float[] array) {
8633  1 shuffle(array, new Random());
8634    }
8635   
8636    /**
8637    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8638    *
8639    * @param array the array to shuffle
8640    * @param random the source of randomness used to permute the elements
8641    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8642    * @since 3.6
8643    */
 
8644  1 toggle public static void shuffle(float[] array, Random random) {
8645  10 for (int i = array.length; i > 1; i--) {
8646  9 swap(array, i - 1, random.nextInt(i), 1);
8647    }
8648    }
8649   
8650    /**
8651    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8652    *
8653    * @param array the array to shuffle
8654    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8655    * @since 3.6
8656    */
 
8657  1 toggle public static void shuffle(double[] array) {
8658  1 shuffle(array, new Random());
8659    }
8660   
8661    /**
8662    * Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
8663    *
8664    * @param array the array to shuffle
8665    * @param random the source of randomness used to permute the elements
8666    * @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
8667    * @since 3.6
8668    */
 
8669  1 toggle public static void shuffle(double[] array, Random random) {
8670  10 for (int i = array.length; i > 1; i--) {
8671  9 swap(array, i - 1, random.nextInt(i), 1);
8672    }
8673    }
8674    }